[R] Selecting n observation

Peter Ehlers ehlers at ucalgary.ca
Thu Oct 11 22:11:29 CEST 2012


On 2012-10-11 12:48, bibek sharma wrote:
> Hello R help,
>   I have a question similar to what is posted by someone before. my
> problem is that Instead of last assessment, I want to choose last two.
>
> I have a data set with several time assessments for each participant.
> I want to select the last assessment for each participant. My dataset
> looks like this:
> ID  week  outcome
> 1   2   14
> 1   4   28
> 1   6   42
> 4   2   14
> 4   6   46
> 4   9   64
> 4   9   71
> 4  12   85
> 9   2   14
> 9   4   28
> 9   6   51
> 9   9   66
> 9  12   84
>
> Here is one solution for choosing last assessment
> do.call("rbind",
>          by(df, INDICES=df$ID, FUN=function(DF) DF[which.max(DF$week), ]))
>    ID week outcome
> 1  1    6      42
> 4  4   12      85
> 9  9   12      84

With the plyr package:

   library(plyr)
   ddply(df, .(ID), function(x) tail(x, 2))

or, slightly simpler:

   ddply(df, .(ID), tail, 2)

Peter Ehlers




More information about the R-help mailing list