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.

330 questions
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
  • 689
  • 4
  • 15
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
  • 7
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
  • 165
  • 3
  • 14
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,447
  • 2
  • 14
  • 25
3
votes
2 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,041
  • 5
  • 29
  • 63
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
3 answers

How to extract the name of a column from a data frame to be used in the loop?

I would like to copy the text of a data frame's column names one-by-one in a for loop. My code seems to return NULL values from the column name argument. More broadly, I want to create a summary by factor of each of several columns. # Create an…
bhbennett3
  • 111
  • 6
2
votes
3 answers

How to subtract every previous rows from the lead row to every five rows in R?

I have a larger data frame that has multiple columns and thousands of rows. I want to replace the value of every lead row by subtracting the previous row value from the lead row for every five rows of the data frame. For example, the first value…
CForClimate
  • 265
  • 2
  • 13
2
votes
2 answers

Compute user defined function output by group in R

I am trying to calculate z-statistic over regular interval of rows. mean = 77 std = 31 samp.45 = rnorm(45,mean,std) z.test = function(a, mu, sd){ zeta = (mean(a) - mu) / (sd / sqrt(length(a))) return(zeta) } z.hypothesis = function(a,…
Manish
  • 344
  • 4
  • 16
2
votes
1 answer

R : Percentile 90% with tapply

I am trying to apply the quantile function to a column (y) of my data (tab) considering the groups (column x): z <- with (tab, tapply (y, x, quantile)) tab       x y 1 1 0.11 2 1 0.07 3 0.04 4 2 0.39 5 2 0.12 6 3 0.21 7 3 0.06 8 3 0.00 9 3 0.12 10…
Ph.D.Student
  • 592
  • 4
  • 21
2
votes
4 answers

Summing rows of a matrix based on column index

I am trying to go from a matrix that has columns that "belong together" to one where the row-sums of the relevant sub-matrices have been formed. I.e. going from [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]…
Björn
  • 392
  • 4
  • 19
2
votes
1 answer

How to use aggregate( ) to count NA values and using tapply() as an alternative

I am new to R and trying to prepare for an exam in R which will take place in one week. On one of the homework questions, I am trying to solve a single problem in as many as ways as possible (preparing more tools always comes in handy in a…
R Newbie
  • 47
  • 5
1 2
3
21 22