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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With