I have a dataframe with two groups and values. I have to find max
value by one group (group
) and discover, to which values does my max
correspond to in the second group (dist
).
# example
df<-data.frame(group = rep(c("a", "b"), each = 5),
val = 1:10,
dist = rep(c("NR", "b1"), 5))
> df
group val dist
1 a 1 NR
2 a 2 b1
3 a 3 NR
4 a 4 b1
5 a 5 NR
6 b 6 b1
7 b 7 NR
8 b 8 b1
9 b 9 NR
10 b 10 b1
I can get the max values by group:
aggregate(val ~ group, df, max)
group val
1 a 5
2 b 10
or by tapply
:
tapply(df$val, df$group, max)
but I need to know, in what "dist" is max
located.
group val dist
1 a 5 NR
2 b 10 b1
How to accomplish this?