[R] Selecting a maximum value in same ID

Pete Brecknock Peter.Brecknock at bp.com
Sat Mar 8 22:21:26 CET 2014


Lee wrote
> 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

How about using ddply?

library(plyr)

txt <-"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"

d <- read.table(textConnection(txt), header = TRUE) 
closeAllConnections() 

ddply(d,~ID,function(x){x[which.max(x$B),]})

# Returns 
  ID  A  B C
1  1 12 36 2
2  2 33 30 2
3  3 83 23 2
4  4 82 36 2
5  5 77 35 2
6  6 76 35 2
7  7 21 30 2
8  8 30 36 2
9  9 21 35 2

HTH

Pete




--
View this message in context: http://r.789695.n4.nabble.com/Selecting-a-maximum-value-in-same-ID-tp4686492p4686500.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list