[R] Selecting a maximum value in same ID
arun
smartpink111 at yahoo.com
Sat Mar 8 17:26:17 CET 2014
Hi,
You could do this either:
DF[unlist(with(DF,tapply(B,list(ID),FUN=function(x) x %in% max(x)))),]
#or
DF[unlist(with(DF,by(B,ID,FUN=function(x) x%in% max(x)))),]
#or
library(plyr)
DF[ddply(DF,.(ID),summarize,B %in% max(B))[,2],]
A.K.
Hi,
I am struggling with this issue and need some helps. The data
set 'DF' includes persons' IDs and other variables A, B, and C. Each
Person has multiple values in A, B, and C. What I am trying to do is 1)
selecting a maximum value of B within same ID, and 2) making a new data
set (DF.2) that select rows aligning with the maximum value of B.
DF and DF.2 are below. I've used functions combining which.max,
subset, and loop, but it did not work. If you have ideas, please help.
> DF
ID A B C
1 12 36 2
1 15 30 2
2 56 11 2
2 33 30 2
3 83 23 2
3 58 7 2
4 75 2 2
4 82 36 2
5 77 35 2
5 75 23 2
6 73 10 2
6 76 35 2
7 75 14 2
7 21 30 2
8 14 11 2
8 46 11 2
8 75 11 2
8 30 36 2
9 21 35 2
9 75 23 2
DF.2
ID A B C
1 12 36 2
2 33 30 2
3 83 23 2
4 82 36 2
5 77 35 2
6 76 35 2
7 21 30 2
8 30 36 2
9 21 35 2
Thank you in advance,
Lee
More information about the R-help
mailing list