On Wed, Oct 15, 2008 at 5:59 AM, Michael Pearmain <[EMAIL PROTECTED]> wrote: > Hi All, > I've been trying to compare if the previous value in a variable is equal to > a binary value..(i.e i want to check if the last event was a yes or no) > > i've been trying to write some code for this, but it seems overly elaborate, > can anyone suggest a better / shorter / neater way? > The below doesn't quite work but shows my idea of splitting by the factor > id, then creating a new vector that is lead, then i was going to use an > ifelse clause.. > > But as i suggested this seem very elaborate.. my sample code below
How about: library(plyr) ddply(DF, .(id), transform, diff = c(NA, tail(time, -1))) This splits the data frame up by id, transforms each piece in the same way as your code (but expressed a little more elegantly) and then joins the pieces back together. More info about plyr is available at http://had.co.nz/plyr Regards, Hadley -- http://had.co.nz/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.