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
0
votes
1 answer

how to use apply instead of a loop in R

I am trying to write an algorithm which is very computationally intensive and hence I want to replace every loop with an apply function. However I am pretty stack in the following loop and I am wondering if anyone has any ideas. bc is a matrix of 0s…
Akis
  • 130
  • 1
  • 8
0
votes
0 answers

R how to pass variable into a function inside tapply

Now my code is as follows: common_timestamps = NULL tapply(data$timestamps, data$date, function(x, common_timestamps ) { if(length(common_timestamps ) == 0) { common_timestamps = x } else { …
hong
  • 51
  • 1
  • 4
0
votes
1 answer

Combine tapply to create a matrix

I need to create a matrix with the columns (Swing, Blues, Contra) and Rows (M, F) using Ht data from a data.frame. I need to create a matrix of NA values and then fill the matrix by row using the outcome from each tapply function. It needs to look…
Vixxen81
  • 33
  • 1
  • 7
0
votes
1 answer

Dealing with grouped dataset in R

I have a dataset like: +----+-------+---------+----------+--+ | id | time | event | timediff | | +----+-------+---------+----------+--+ | 1 | 15.00 | install | - | | | 1 | 15.30 | sale | 00.30 | | | 1 | 16.00 | sale | 00.30…
chopin_is_the_best
  • 1,951
  • 2
  • 23
  • 39
0
votes
1 answer

Table of regression predictions for all pairings of two factors

For an assignment we are asked to provide predictions for all pairing of two factor variables in a table. I have two factors and a linear model. I would like to output a table such that rows are levels of one factor, columns are the levels of the…
Syzorr
  • 587
  • 1
  • 5
  • 17
0
votes
1 answer

tapply, plotting, length doesn't match

I am trying to generate a plot from a dataset of 2 columns - the first column contains distances and the second contains correlations of something measured at those distances. Now there multiple entries with the same distance but different…
Tapan Goel
  • 11
  • 1
0
votes
2 answers

sorting rows in matrix in r

I have a simple matrix in r, produced with tapply: > test_fixer H01 H02 H03 M01 M02 M03 M04 M05 M06 N 0.2205882 0.0750000 0.4027778 0.2926829 0.3207547 0.3142857 0.1627907 0.2653061 0.2285714 Y…
PSR
  • 1
0
votes
2 answers

Multiple IDs list in tapply

I want to calculate the mean of a variable in different groups. To define the groups I want to utilise the combination of two column in the data frame that describe the single observations: a<-sample(12) b<-sample(-100:100,…
Al14
  • 1,734
  • 6
  • 32
  • 55
0
votes
1 answer

Is there a way to vectorize this operation using xapply in R

I have a vector a <- c("there and", "walk and", "and see", "go there", "was i", "and see", "i walk", "to go", "to was") and a data frame bg where bg <- data.frame(term=c("there and", "walk and", "and see", "go there", "was i", "and see", "i walk",…
Tinniam V. Ganesh
  • 1,979
  • 6
  • 26
  • 51
0
votes
1 answer

How to Use nlsLM function together with one of the apply family function in R

I need a guide about how to do multiple regression columnwise. I have a data frame where I want to get each columns fitting coefficients separately. So far I can have those results for only one column. What I tried so far Maybe assigning the…
Alexander
  • 4,527
  • 5
  • 51
  • 98
0
votes
1 answer

How to pass decreasing and/or na.last argument to sort through tapply in R

I am teaching myself the basics of R and have been encountering trouble using the function tapply when passing the sort function while trying to use non-default optional arguments for sort. Here is an example of the trouble I am facing: Given the…
Tom
  • 593
  • 4
  • 9
0
votes
0 answers

Aggregate data.table with weights in R

I am new using data.table, and it is sometimes confusing. I have a dataset that looks like this (data.table) GEO ANC WG V1 .... P1 A1 w1 v11 .... P1 A2 w2 v12 .... P2 A1 w3 v13 .... P3 A1 w4 v14 .... Here's a…
Doon_Bogan
  • 359
  • 5
  • 17
0
votes
1 answer

How to perform t-tests for each level of a factor with tapply

My data and code are like this: my_vector <- rnorm(150) my_factor1 <- gl(3,50) my_factor2 <- gl(2,75) tapply(my_vector, my_factor1, function(x) t.test(my_vector~my_factor2, paired=T)) I want to do a separate t-test for each level of my_factor1,…
Joeri
  • 157
  • 2
  • 12
0
votes
1 answer

Applying consecutive functions to a dataframe and outputting results of each into a table

I have a large dataframe with a similar format to below (running to ~200 compounds). +-----------+----------+------------+ | Treatment | Compound | Proportion | +-----------+----------+------------+ | A | wax | 0.095 | | A …
embee
  • 17
  • 5
0
votes
1 answer

tapply returning NA despite na.rm=T being in the argument

I would like to generate a summary(mean) 0f a subgroup of a dataset in R using the tapply function. The dataset= VehicleData". I would like to calculate the mean for the response variable,"HWY_MPG" after the data has been grouped into 2 factors;…