Questions tagged [tapply]

tapply is a function in the R programming language for apply a function to subsets of a vector.

tapply is a function in the R programming language for apply a function to subsets of a vector. A vector is broken in to subsets, potentially of different lengths (aka a ragged array) based on the values of one or more other vector. The second vector is either already a factor or coerced to be a factor by as.factor. A function is applied to each of these subsets. tapply then returns either an array or a list, depending on the output of the function.

354 questions
2
votes
1 answer

Summing overlapping rows from a single column in R

I am trying to sum values from the same column across multiple rows in R, but each row falls into 1 or 2 of the desired output rows, so I have struggled to use ddply or tapply successfully. I have triangular transect data, where sample points were…
Andrew
  • 516
  • 1
  • 6
  • 17
2
votes
1 answer

Converting time series to data frame, matrix, or table

With reference to this question: Transforming a time-series into a data frame and back I have a list of monthly averages that start in May 2012 and go through May 2015. It looks like this initially: head (AVG_LOSCAT2) month AVG_LOSCAT YEAR…
rstunt
  • 35
  • 1
  • 5
2
votes
1 answer

Averaging values between paired columns across a large data frame

I have a dataframe consisting of a series of paired columns. Here is a small example. df1 <- as.data.frame(matrix(sample(0:1000, 36*10, replace=TRUE), ncol=1)) df2 <- as.data.frame(rep(1:12, each=30)) df3 <- as.data.frame(matrix(sample(0:500,…
James White
  • 705
  • 2
  • 7
  • 20
2
votes
0 answers

Apply fitdistr function to a whole data frame using tapply

I have a huge data frame but I made a reproducible example with only 100 rows. The objective of my code is to calculate per value of a column (here V2), the mean and standard deviation given by the function fitdistr. The density function I use as…
user3443183
  • 115
  • 6
2
votes
1 answer

Tapply over matrix using matrix math

All, I have the following code, I'd like to make it generalized for more clusters, ie C clusters. Is there a way to do this without a loop? Here, the rows of X correspond to variables x1,x2, and T is a linear transformation of X.…
Rik
  • 1,870
  • 3
  • 22
  • 35
2
votes
1 answer

why sapply() and tapply() have the same result, but not identical?

Here's my code pulse <- round(rnorm(22, 70, 10 / 3)) + rep(c(0, 5), c(10, 12)) group <- rep(c("A", "B"), c(10, 12)) tapply(pulse, group, length) A B 10 12 list<-split(pulse,group) sapply(list,length) A B 10 12 identical(tapply(pulse, group,…
SongTianyang
  • 139
  • 3
  • 9
2
votes
2 answers

R - Is the result of tapply always in alphabetical order

I work with the dataframe df Name = c("Albert", "Caeser", "Albert", "Frank") Earnings = c(1000,2000,1000,5000) df = data.frame(Name, Earnings) Name Earnings Albert 1000 Caesar 2000 Albert 1000 Frank 5000 If I use the…
rmuc8
  • 2,869
  • 7
  • 27
  • 36
2
votes
1 answer

How to fit the outcome of tapply function into different length vector?

For example I got a set of data with 3 levels,namely 1,2,3 a and b are two 3*5 matrices as shown below: a <- matrix(c(0.1,0.2,0.3,0.3,0.2,0.1,0.1,0.2,0.3,0.3,0.2,0.1,0.1,0.2,0.3),3) # [,1] [,2] [,3] [,4] [,5] #[1,] 0.1 0.3 0.1 0.3 …
2
votes
1 answer

Apply a multivariate function over two lists

I have a mapping function as follows, sub.func <- function(x,y){ if(agrepl(x,y)){ return(x) } else{ return(y) } } now I want to apply this to compare two lists of different size, say myList1 and myList2, such that for each element…
DonDyck
  • 1,451
  • 5
  • 20
  • 35
2
votes
3 answers

R: Is there a simple way to make ave() work for matrices?

Sorry if this will seem trivial, but after searching the internet for some time I couldn't come upon a solution. I have a matrix and a factor vector associated with columns. The goal is to get rowMeans for all factors separately and maintain the…
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
2
votes
0 answers

barplot of frequency by binary

This is from Wikipedia. I want to make similar barplot with data below. audit<-structure(list(ID = c(1004641L, 1010229L, 1024587L, 1038288L, 1044221L, 1047095L, 1047698L, 1053888L, 1061323L, 1062363L), Age = c(38L, 35L, 32L, 45L, 60L, 74L, 43L,…
Doo Hyun Shin
  • 297
  • 3
  • 15
2
votes
2 answers

Rank a sorted dataset using apply function

My dataframe looks like this: head(temp$HName) [1] "UNIVERSITY OF TEXAS HEALTH SCIENCE CENTER AT TYLER" [2] "METHODIST HOSPITAL,THE" [3] "TOMBALL REGIONAL MEDICAL CENTER" [4] "METHODIST SUGAR LAND…
2
votes
2 answers

Subtract minimum of the column based on other column

I have a dataframe as follows: d year total file 1999 3931.12000 A 2002 4273.71020 A 2005 4601.41493 A 2008 4101.32100 A 1999 346.82000 B 2002 134.30882 B 2005 130.43038 B 2008…
JelenaČuklina
  • 3,574
  • 2
  • 22
  • 35
2
votes
1 answer

Summarizing Latitude, Longitude, and Counts Data for ggplot Usage

I have been provided with some customer data in Latitude, Longitude, and Counts format. All the data I need to create a ggplot heatmap is present, but I do not know how to put it into the format ggplot requires. I am trying to aggregate the data by…
rucker
  • 393
  • 3
  • 13
2
votes
7 answers

How do I do a conditional sum which only looks between certain date criteria

Say I have data that looks like date, user, items_bought, event_number 2013-01-01, x, 2, 1 2013-01-02, x, 1, 2 2013-01-03, x, 0, 3 2013-01-04, x, 0, 4 2013-01-04, x, 1, 5 2013-01-04, x, 2, 6 2013-01-05, x, 3, 7 2013-01-06, x, 1, 8 2013-01-01, y, 1,…
shecode
  • 1,716
  • 6
  • 32
  • 50