Hi, Try: evt_c.1<- read.table(text=" patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 2 2010-5 2 2010-6 2 2010-7 ",sep="",header=TRUE,stringsAsFactors=FALSE) lst1<-split(evt_c.1,evt_c.1$patient_id) res<-do.call(rbind,lapply(lst1,function(x) {x1<-as.numeric(gsub(".*\\-","",x[,2])); x$t<-c(0,cumsum(diff(x1)));x})) row.names(res)<-1:nrow(res) res # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 2 2010-5 0 #6 2 2010-6 1 #7 2 2010-7 2
#or library(plyr) res2<-mutate(evt_c.1,t=ave(as.numeric(gsub(".*\\-","",responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x))))) res2 # patient_id responsed_at t #1 1 2010-5 0 #2 1 2010-7 2 #3 1 2010-8 3 #4 1 2010-9 4 #5 2 2010-5 0 #6 2 2010-6 1 #7 2 2010-7 2 identical(res,res2) #[1] TRUE A.K. ________________________________ From: GUANGUAN LUO <guanguan...@gmail.com> To: arun <smartpink...@yahoo.com> Sent: Wednesday, April 17, 2013 8:32 AM Subject: Re: how to change the date into an interval of date? thank you, and now i've got a table like this > dput(head(evt_c.1,5)) structure(list(responsed_at = c("2010-05", "2010-07", > "2010-08", "2010-10", "2010-11"), patient_id = c(2L, 2L, 2L, 2L, 2L), number = c(1, 2, 3, 4, 5), response_id = c(77L, 1258L, 2743L, 4499L, 6224L), session_id = c(2L, 61L, 307L, 562L, 809L), login = c(3002, 3002, 3002, 3002, 3002), clinique_basdai.fatigue = c(4, 5, 5, 6, 4), which i want is to add a column "t", for example now my table is like this: patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 2 2010-5 2 2010-6 2 2010-7 after add the column "t" paient_id responsed_at t 1 2010-5 0 1 2010-7 2 1 2010-8 3 1 2010-9 4 2 2010-5 0 2 2010-6 1 2 2010-7 2 Le 17 avril 2013 14:23, arun <smartpink...@yahoo.com> a écrit : Hi, >format() is one way. >library(zoo) > as.yearmon(dat1$responsed_at) >#[1] "May 2010" "Jul 2010" "Aug 2010" "Oct 2010" "Nov 2010" "Dec 2010" > #[7] "Jan 2011" "Feb 2011" "Mar 2011" "Apr 2011" "Jun 2011" "Jul 2011" >#[13] "Aug 2011" "Sep 2011" "Oct 2011" "Nov 2011" "Dec 2011" "Jan 2012" >#[19] "Mar 2012" "May 2010" >A.K. > > > > ______________________________________________ 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.