m0h3n m0h3n - 1 year ago 76
R Question

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


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,

And a vector of indices (possibly with

inds <- c(2,1,NA)

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

For example, based on
, we want the second element of
in group
, first element in group
in group
. So the result would be:

#[1] 1 3 NA

lmo lmo
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download