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
3
votes
3 answers

Calculate accuracy by groups

I have a data frame which looks like this: df<- data.frame("iteration" = c(1,1,1,1,1,1), "model" = c("RF","RF","RF","SVM", "SVM","SVM"), "label" = c(0,0,1,0,0,1), "prediction" = c(0,1,1,0,1,1)) iteration model label prediction 1 …
Saul Garcia
  • 890
  • 2
  • 9
  • 22
3
votes
1 answer

Using tapply on data with counts to add zeros and NAs

I have a DB composed of: Species ID (as factor), counts, site, visit, year. Find a subset in here [Google Drive] I want to create a 4D array with the dimensions: species, site, visit and year. Counts as cell values. For which I am using the…
YMC
  • 63
  • 6
3
votes
1 answer

How to separate factor interactions in R

I recently had to graph some data based on an interaction of factors and I found it more difficult than I felt something this common should be in R. I suspect I'm missing something. Let's say I have a vector of 30 numbers along with a pair of…
pglezen
  • 961
  • 8
  • 18
3
votes
3 answers

error in tapply: arguments must have same length

I have imported a set of data into R as a data frame from a .csv file. Originally, I had an error message as follows: > data<-read.csv("D:/research/PhD 2014/Data/anaesthesia trials/anaesthesia times.csv", header=TRUE) > str(data) 'data.frame': …
Daniel Svozil
  • 85
  • 1
  • 1
  • 9
3
votes
3 answers

How to add tapply results to an existing data frame

I would like to add tapply results to the original data frame as a new column. Here is my data frame: dat <- read.table(text = " category birds wolfs snakes yes 3 9 7 no 3 …
migdal menora
  • 169
  • 4
  • 16
3
votes
1 answer

understanding difference in results between dplyr group_by vs tapply

I was expecting to see the same results between these two runs, and they are different. Makes me question if I really understand what how the dplyr code is working (I have read pretty much everything I can find about dplyr in the package and…
Michael Bellhouse
  • 1,547
  • 3
  • 14
  • 26
3
votes
3 answers

tapply function complains that args are unequal length yet they appear to match

Here is the failing call, error messages and some displays to show the lengths in question: it <- tapply(molten, c(molten$Activity, molten$Subject, molten$variable), mean) # Error in tapply(molten, c(molten$Activity, molten$Subject,…
gregbowman
  • 31
  • 1
  • 1
  • 2
3
votes
1 answer

How can i do tapply with filter on one of the variables

I'm using the tapply function in order to get the count of variable over another variable. Here is the line of code: tapply(vip$VAR1,vip$VAR2,length) However, I would like to filter only observations that have the value "1" on vip$VAR1, can I do…
mql4beginner
  • 2,193
  • 5
  • 34
  • 73
3
votes
1 answer

Plotting data from a tapply output in R

I'm a real beginner and trying to analyze some data on the material loss on some metal tubes for my master thesis. I want to compare the standard deviation of the material loss over an interval for different tubes. I created some sub matrices and…
r.j.mendel
  • 65
  • 1
  • 5
3
votes
3 answers

Apply the corr function to a matrix using levels of a factor?

I'm trying using the corr() function to calculate weighted ponderations. The way it works is the first argument should be a matrix with two columns corresponding to the two variables whose correlation we wish to calculate and the second a vector of…
Tom
  • 61
  • 6
3
votes
1 answer

Standard errors of each observation among grouped data in data frame

I have a data frame where I'd like to calculate the standard error of observations grouped by factors in three columns. The standard deviation and standard error of the mean of the groups have been calculated like this, using tapply: aveResponse <-…
user1214160
  • 31
  • 1
  • 3
2
votes
2 answers

improvement on tapply (shifting groups of vectors)

The order of the return object from tapply() is ambiguous, so I've started to worry about this bit of code: #d <- data.frame(value = c(1,2,3,5), # source = c("a","a","b","b")) d$value <- unlist(tapply(d$value, d$source, function(v)…
Taylor
  • 1,797
  • 4
  • 26
  • 51
2
votes
1 answer

Is there a function in R to connect tapply with round?

Probably the solution is simple, but I can't find it right now. I like to use tapply in descriptive-statistical analyses. For example: tapply(dataframe1$Means, dataframe2$gender, function(x) { c( "mean" = mean(x, na.rm = TRUE), "sd" =…
formatc
  • 35
  • 5
2
votes
1 answer

Count values greater than x within subsets of a matrix?

I have a matrix (49 rows x 533 columns) and the columns are subsetted into 5 "subtypes". For each row, I want to count how many values are greater than 1 within each subtype e.g. if I have subsets A,B,C,D,E: "In row (i) how many of the values in…
Violet
  • 21
  • 1
2
votes
2 answers

why `stack` cannot work on the result of `tapply`?

Assuming I have a data frame df > dput(df) structure(list(x = c("X", "X", "X", "Y", "Y", "Z", "Z", "Z"), y = c("A", "B", "C", "B", "C", "A", "C", "D")), class = "data.frame", row.names = c(NA, -8L)) > df x y 1 X A 2 X B 3 X C 4 Y B 5 Y C 6 Z…
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1 2
3
23 24