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
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 84With the plyr package: library(plyr) ddply(df, .(ID), function(x) tail(x, 2)) or, slightly simpler: ddply(df, .(ID), tail, 2) Peter Ehlers
On Oct 11, 2012, at 12:48 PM, 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), ]))Why wouldn't the solution be something along the lines of: do.call("rbind", by(df, INDICES=df$ID, FUN=function(DF) tail(DF, 2) ))> ID week outcome > 1 1 6 42 > 4 4 12 85 > 9 9 12 84 > >David Winsemius, MD Alameda, CA, USA