[R] Hello R User
arun
smartpink111 at yahoo.com
Fri Dec 14 17:21:54 CET 2012
HI,
Try this:
dat1<-read.table(text="
ID Time
1 3
1 6
1 7
1 10
1 16
2 12
2 18
2 19
2 25
2 28
2 30
",sep="",header=TRUE)
dat1$Time1<-ave(dat1$Time,dat1$ID,FUN=function(x) c(0,diff(x)))
head(dat1,3)
# ID Time Time1
#1 1 3 0
#2 1 6 3
#3 1 7 1
#or
dat2<-unsplit(lapply(split(dat1,dat1$ID),function(x) {x$Time<-c(0,diff(x[,2])); return(x)}),dat1$ID)
head(dat2,3)
# ID Time
#1 1 0
#2 1 3
#3 1 1
A.K.
----- Original Message -----
From: bibek sharma <mbhpathak at gmail.com>
To: R help <r-help at r-project.org>
Cc:
Sent: Friday, December 14, 2012 10:51 AM
Subject: [R] Hello R User
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 at 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.
More information about the R-help
mailing list