dataset<-data.frame(id=c(1,1,2,3,3,3),time=c(3,5,1,2,4,6)) dataset id time 1 1 3 2 1 5 3 2 1 4 3 2 5 3 4 6 3 6 ids<-unique(dataset$id) for(id in ids){ + dataset$time[dataset$id==id]<-c(0,diff(dataset$time[dataset$id==id])) + } dataset id time 1 1 0 2 1 2 3 2 0 4 3 0 5 3 2 6 3 2
might not be the fastest though. On 14.12.2012, at 16:51, bibek sharma wrote: > Hello R User, > In the sample data given below, time is recorded for each id > subsequently. For the analysis, for each id, I would like to set 1st > recorded time to zero and thereafter find the difference from previous > time. I.e. for ID==1, I would like to see Time=0,3,1,3,6. This needs > to be implemented to big data set. > Any suggestions are much appreciated! > Thanks, > Bibek > > ID Time > 1 3 > 1 6 > 1 7 > 1 10 > 1 16 > 2 12 > 2 18 > 2 19 > 2 25 > 2 28 > 2 30 > > ______________________________________________ > 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. ______________________________________________ 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.