Hi, I hope this is what you are looking for: library(plyr) mutate(evt_c.1,t=ave(as.numeric(gsub(".*\\-","",responsed_at)),patient_id,gsub("-.*","",responsed_at),FUN=function(x) c(0,cumsum(diff(x))))) # 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 1 2010-12 7 #6 1 2011-1 0 #7 1 2011-2 1 #8 2 2010-5 0 #9 2 2010-6 1 #10 2 2010-7 2 #11 3 2010-1 0 #12 3 2010-2 1 #13 3 2010-4 3 #14 3 2010-5 4 #15 4 2011-01 0 #16 4 2011-03 2 #17 5 2012-04 0 #18 5 2012-06 2 A.K.
________________________________ From: GUANGUAN LUO <guanguan...@gmail.com> To: arun <smartpink...@yahoo.com> Sent: Wednesday, April 17, 2013 9:21 AM Subject: Re: how to change the date into an interval of date? evt_c.1<- read.table(text=" patient_id responsed_at 1 2010-5 1 2010-7 1 2010-8 1 2010-9 1 2010-12 1 2011-1 1 2011-2 2 2010-5 2 2010-6 2 2010-7 3 2010-1 3 2010-2 3 2010-4 3 2010-5 4 2011-01 4 2011-03 5 2012-04 5 2012-06 ",sep="",header=TRUE, stringsAsFactors=FALSE) mutate(evt_c.11,t=ave(as.numeric(gsub(".*\\-","",responsed_at)),patient_id,FUN=function(x) c(0,cumsum(diff(x))))) 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 1 2010-12 7 6 1 2011-1 -4 7 1 2011-2 -3 8 2 2010-5 0 9 2 2010-6 1 10 2 2010-7 2 11 3 2010-1 0 12 3 2010-2 1 13 3 2010-4 3 14 3 2010-5 4 15 4 2011-01 0 16 4 2011-03 2 17 5 2012-04 0 18 5 2012-06 2 This is my problem. ______________________________________________ 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.