Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the nth value of aggregated column after group by in R

Given df as follows:

  # group value
# 1     A     8
# 2     A     1
# 3     A     7
# 4     B     3
# 5     B     2
# 6     B     6
# 7     C     4
# 8     C     5

df <- structure(list(group = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L), .Label = c("A", "B", "C"), class = "factor"), value = c(8L, 
1L, 7L, 3L, 2L, 6L, 4L, 5L)), .Names = c("group", "value"), class = "data.frame", row.names = c(NA, 
-8L))

And a vector of indices (possibly with NA):

inds <- c(2,1,NA)

How we can get the nth element of column value per group, preferably in base R?

For example, based on inds, we want the second element of value in group A, first element in group B, NA in group C. So the result would be:

#[1] 1 3 NA
like image 211
989 Avatar asked Jan 06 '23 05:01

989


1 Answers

Here is a solution with mapply and split:

mapply("[", with(df, split(value, group)), inds)

which returns a named vector

 A  B  C 
 1  3 NA

with(df, split(value, group)) splits the data frame by group and returns a list of data frames. mapply takes that list and "inds" and applies the subsetting function "[" to each pairs of arguments.

like image 60
lmo Avatar answered Jan 07 '23 19:01

lmo