Re: [R] within group sequential subtraction

2011-03-10 Thread jim holtman
Try this: > data$diff <- ave(as.numeric(data$date_obs), data$group, FUN=function(x)c(NA, > diff(x))) > data group date_obs diff 1 IND1 1987-09-17 NA 2 IND1 1989-05-04 595 3 IND2 1997-04-30 NA 4 IND2 2008-11-03 4205 5 IND2 2009-05-08 186 6 IND3 1984-01-17 NA 7 IND4 1996-

Re: [R] within group sequential subtraction

2011-03-10 Thread Joshua Wiley
Dear Natalie, I am sure there are other ways, but one way you can do this is by applying diff() to each group using tapply() or by(). Because those return lists, if you want to add it back into your data frame, you can wrap the whole call in unlist(). Here is an example: dat <- structure(list(g

[R] within group sequential subtraction

2011-03-10 Thread natalie.vanzuydam
Hi Everyone, I would like to do sequential subtractions within a group so that I know the time between separate observations for a group of individuals. My data: data <- structure(list(group = c("IND1", "IND1", "IND2", "IND2", "IND2", "IND3", "IND4", "IND5", "IND6", "IND6"), date_obs = struc