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
5
votes
2 answers

R: How do you apply grep() in lapply()

I would like to apply grep() in R, but I am not really good in lapply(). I understand that lapply is able to take a list, apply function to each members and output a list. For instance, let x be a list consists of 2 members. >…
HNSKD
  • 1,614
  • 2
  • 14
  • 25
5
votes
2 answers

R quantile by groups with assignments

I have the following df: group = rep(seq(1,3),30) variable = runif(90, 5.0, 7.5) df = data.frame(group,variable) I need to i) Define quantile by groups, ii) Assign each person to her quantile with respect to her group. Thus, the output would look…
Jb_Eyd
  • 635
  • 1
  • 7
  • 20
5
votes
1 answer

R - Loop through different matrices without using loop ! Help to simply a code

So I have two separate matrix (mat1 and mat2) and I need to go through them in order to make a check. I need to store the results into a third matrix. I feel that my code is very long for the purpose. I wanted to have some of your suggestion to…
giac
  • 4,261
  • 5
  • 30
  • 59
4
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
  • 335
  • 5
  • 19
4
votes
0 answers

R tapply: different R releases produce different outputs

The Problem This a simple tapply example: z=data.frame(s=as.character(NA), rows=c(1,2,1), cols=c(1,1,2), stringsAsFactors=FALSE) tapply(z$s, list(z$rows, z$cols), identity) On R (Another Canoe) v3.3.3 (2017-03-06) for Windows, it brings: # 1 2…
antonio
  • 10,629
  • 13
  • 68
  • 136
4
votes
3 answers

R function which.max with tapply

I am trying to make a data frame with the maximum over records by a factor. I would like a data frame with 4 rows (one for each G) with the max for X in that group and the corresponding Y value. I know I could write a loop but would rather…
LoveMeow
  • 1,141
  • 2
  • 15
  • 26
4
votes
1 answer

What is the difference of tapply and aggregate in R?

Aaa <- data.frame(amount=c(1,2,1,2,1,1,2,2,1,1,1,2,2,2,1), card=c("a","b","c","a","c","b","a","c","b","a","b","c","a","c","a")) aggregate(x=Aaa$amount, by=list(Aaa$card), FUN=mean) ## Group.1 x ## 1 a 1.50 ## 2 …
Neo XU
  • 91
  • 2
  • 5
3
votes
1 answer

What is the Base R equivalent of this dplyr group_by code?

The R4DS book has the following code block: library(tidyverse) by_age2 <- gss_cat %>% filter(!is.na(age)) %>% count(age, marital) %>% group_by(age) %>% mutate(prop = n / sum(n)) Is there a simple equivalent to this code in base R? The…
J. Mini
  • 1,868
  • 1
  • 9
  • 38
3
votes
4 answers

Mean with condition for multiple columns in r

Let's use mtcars to explain the situation. What I want to do is the same below for multiple columns. To have the mean of a column qsec (in the example) regarding another column with a specific value (4 and 6, in the example below). I'll compare the…
ivan lange
  • 55
  • 1
  • 3
3
votes
1 answer

How can I summarise a factor or character variable?

I would like to 'summarise' a factor variable in R, so that for each record I know what factor levels are present. Here is a simplified example dataframe: df <- data.frame(record= c("a","a","b","c","c","c"), species = c("COD", "SCE", "COD",…
Shep
  • 41
  • 1
  • 1
  • 5
3
votes
5 answers

Get sum of every n th column for each individual and create new data frame in r

Having searched for similar posts, I am posting my question. I have monthly rainfall variables for several years for each site. I need to calculate monthly average rainfall over the years. I have given a simple data frame as follows. I need to…
sriya
  • 179
  • 1
  • 2
  • 7
3
votes
1 answer

R - tapply column mean, returning logical array

I have a data frame. I am trying to use the tapply function to find the average of one column when the values of a second column are equal to a given value. I want tapply to return the value of the mean, but it is returning a logical array (FALSE -…
Lama Kaysi
  • 69
  • 4
3
votes
1 answer

Calculating quintile based scores on R

I have a dataframe with year (2006 to 2010), 4 industry sectors, 150 firm names and the net income of these firms. In total I have 750 observations, one for each firm for each year. I want to give scores to firms for their income within each…
Piyush Shah
  • 301
  • 4
  • 15
3
votes
3 answers

Computing pairwise Hamming distance between all rows of two integer matrices/data frames

I have two data frames, df1 with reference data and df2 with new data. For each row in df2, I need to find the best (and the second best) matching row to df1 in terms of hamming distance. I used e1071 package to compute hamming distance. Hamming…
alaj
  • 187
  • 1
  • 10
3
votes
1 answer

Apply custom function to each subset of a data frame and result a dataframe

It may be asked many times here, but i am not able to relate it to any since my function returns data frame. I have my custom function which builds model and outputs a data frame with slope(coeff2) in one column, intercept(coeff1) in another…
ds_user
  • 2,139
  • 4
  • 36
  • 71
1
2
3
23 24