Hi, I am not sure I understand your question correctly. dat1<- read.table(text=" id??????????? responsed_at???????????????? number_of_connection????????????????? scores 1????????????????? 12-01-2010?????????????????????????????????? 1????????????????????????????????????????????? 2 1????????????????? 15-02-2010?????????????????????????????????? 2????????????????????????????????????????????? 3 1?????????????????? 01-04-2010????????????????????????????????? 3????????????????????????????????????????????? 2 ",sep="",header=TRUE,stringsAsFactors=FALSE) datNew<- dat1 datNew$responsed_at<- gsub("^.*\\-(.*\\-.*)","\\1", datNew$responsed_at) dat2<-data.frame(responsed_at=format(seq(from=as.Date("01-01-2010",format="%d-%m-%Y"),to=as.Date("01-04-2010",format="%d-%m-%Y"),by="month"),"%m-%Y")) res<-merge(datNew,dat2,all=TRUE,by="responsed_at") ?res$responsed_at[is.na(res$id)]<-NA res$responsed_at[!is.na(res$responsed_at)]<- paste(gsub("(.*)\\-.*\\-.*","\\1",dat1$responsed_at),"-",res$responsed_at[!is.na(res$responsed_at)],sep="") res$number_of_month<- seq_len(nrow(res))-1 res #? responsed_at id number_of_connection scores number_of_month #1?? 12-01-2010? 1??????????????????? 1????? 2?????????????? 0 #2?? 15-02-2010? 1??????????????????? 2????? 3?????????????? 1 #3???????? <NA> NA?????????????????? NA???? NA?????????????? 2 #4?? 01-04-2010? 1??????????????????? 3????? 2?????????????? 3>Dear experts: >?>I have my table like this:> >id ? ? ? ? ? ?responsed_at ? ? ? ? ? ? ? ? number of connection ? ? ? ? ? ? ? ? ?scores >1 ? ? ? ? ? ? ? ? ?12-01-2010 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 >1 ? ? ? ? ? ? ? ? ?15-02-2010 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3 >1 ? ? ? ? ? ? ? ? ? 01-04-2010 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 > >I want to have one table like this: > >id ? ? ? ? ? ? responsed_at ? ? ? ? ? ? ?number of month ? ? ? ? ? ? ? number of connection ? ? ? ? ? ? ? scors >1 ? ? ? ? ? ? ? 12-01-2010 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0 ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2>1 ? ? ? ? ? ? ? ?15-02-2010 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1 ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3>1 ? ? ? ? ? ? ? ? NA ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?NA ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NA>1 ? ? ? ? ? ? ? ?01-04-2010 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3 ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2>I want to plus a column indicating the number of month when patients responsed. ?How can I realize that in R? >The rule for number of month is the date in one month, ex: from thefirst day to the last day of one month count one number of month.>Thank you in avance.
Hi, Please check your dput(). By using your dput() output, I am getting: $patient_id [1] 2 2 2 2 3 3 3 3 $responsed_at [1] 14755 14797 14835 14883 14755 14789 14826 14857 $number [1] 1 2 3 4 1 2 3 4 $score [1] 1 1 2 3 1 5 4 5 $.Names [1] "patient_id"?? "responsed_at" "number"?????? "scores"????? $row.names [1] NA -8 $class [1] "data.frame" It looks like the image from shared also showed the same output.? I am not using RStudio.? So, I don't know what is wrong. #the dput should be:dat1<- structure(list(patient_id = c(2,2,2,2,3,3,3,3), ?????????????? responsed_at = c(14755,14797,14835,14883,14755,14789,14826,14857), ?????????????? number = c(1,2,3,4,1,2,3,4), score=c(1,1,2,3,1,5,4,5)),? ?????????????? .Names = c("patient_id","responsed_at", "number", "scores"),row.names=c(NA,-8L),class = "data.frame") dat1 #? patient_id responsed_at number scores #1????????? 2??????? 14755????? 1????? 1 #2????????? 2??????? 14797????? 2????? 1 #3????????? 2??????? 14835????? 3????? 2 #4????????? 2??????? 14883????? 4????? 3 #5????????? 3??????? 14755????? 1????? 1 #6????????? 3??????? 14789????? 2????? 5 #7????????? 3??????? 14826????? 3????? 4 #8????????? 3??????? 14857????? 4????? 5 library(zoo) dat1$responsed_at<-as.Date(dat1$responsed_at) ?dat1 #? patient_id responsed_at number scores #1????????? 2?? 2010-05-26????? 1????? 1 #2????????? 2?? 2010-07-07????? 2????? 1 #3????????? 2?? 2010-08-14????? 3????? 2 #4????????? 2?? 2010-10-01????? 4????? 3 #5????????? 3?? 2010-05-26????? 1????? 1 #6????????? 3?? 2010-06-29????? 2????? 5 #7????????? 3?? 2010-08-05????? 3????? 4 #8????????? 3?? 2010-09-05????? 4????? 5 ?str(dat1) #'data.frame':??? 8 obs. of? 4 variables: # $ patient_id? : num? 2 2 2 2 3 3 3 3 # $ responsed_at: Date, format: "2010-05-26" "2010-07-07" ... # $ number????? : num? 1 2 3 4 1 2 3 4 # $ scores????? : num? 1 1 2 3 1 5 4 5 A.K. ________________________________ From: GUANGUAN LUO <guanguanluo at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Tuesday, April 16, 2013 10:49 AM Subject: Re: how to change the date into an interval of date? hi, ?dput(head(data,8)) structure(list(patient_id = c(2,2,2,2,3,3,3,3), ?????????????? responsed_at = c(14755,14797,14835,14883,14755,14789,14826,14857), ?????????????? number = c(1,2,3,4,1,2,3,4), score=c(1,1,2,3,1,5,4,5),? ?????????????? .Names = c("patient_id", ? "responsed_at", "number", "scores"),? class = "data.frame")) like this? i use R studio, there are 4 windows , window of results is the output. 2013/4/16 arun <smartpink111 at yahoo.com> HI,>Please dput() your dataset as in my previous reply.? This is image and it is twice or thrice the work for me to convert it to readable form. > >http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example > > >Also,? you didn't answer to my question: >"I didn't understand which one is "window of results" and which is "my tables"." > > > > > >________________________________ > From: GUANGUAN LUO <guanguanluo at gmail.com> >To: smartpink111 at yahoo.com >Sent: Tuesday, April 16, 2013 10:10 AM >Subject: Re: how to change the date into an interval of date? > > > >? >patient_id >number >response_id >session_id >responsed_at >login >clinique_basdai.fatigue > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >1 2 1 77 2 14755 3002 4 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >2 2 2 1258 61 14797 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >3 2 3 2743 307 14835 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >4 2 4 4499 562 14883 3002 6 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >5 2 5 6224 809 14916 3002 4 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >6 2 6 7708 1024 14949 3002 3 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >7 2 7 9475 1224 14985 3002 3 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >8 2 8 11362 1458 15020 3002 4 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >9 2 9 13417 1688 15055 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >10 2 10 15365 1959 15090 3002 4 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >11 2 11 17306 2211 15126 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >12 2 12 19073 2449 15160 3002 3 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >13 2 13 20679 2677 15193 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >14 2 14 22294 2883 15228 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >15 2 15 24097 3082 15265 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >16 2 16 25670 3304 15299 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >17 2 17 27180 3500 15332 3002 7 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >18 2 18 28705 3694 15365 3002 6 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >19 2 19 30445 3883 15403 3002 5 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >20 3 1 57 3 14755 3005 7 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >21 3 2 724 62 14789 3005 2 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >22 3 3 2381 168 14826 3005 2 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >23 3 4 3556 510 14857 3005 4 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >24 3 5 5163 678 14895 3005 7 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >25 3 6 6784 894 14928 3005 3 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >26 3 7 8341 1100 14961 3005 3 > > >Hello, this is my table, and i want to work with a format "yyyy-mm-dd", but you see the column "responsed_at" are the grand numbers. >I want to change these into normal format. >I don't know whether it owe to the wrong format that i? can not merge two tables together. >Thank you so much. > >Guanguan > > >2013/4/16 <smartpink111 at yahoo.com> > > >>Hi, >>I didn't understand which one is "window of results" and which is "my tables". ?According to your post, it looks like one of them is in the correct date format. ?So, you wanted to work on the dataset with the incorrect date format? >>Please dput(head(dataset,20)). ?By using these images, it takes so much time to type the data as in your image and it won't give much details regarding the structure of your dataset. >>A.K. >> >> >> >><quote author='mimorsa'> >>Hello AK, >>?Thank you so much for your help. >>I've tried your method to my tables, and I think I had not expressed clearly >>my problems. >>Sorry to trouble you so much. >> >>In reality, my table is like this: >> >> >> >> >>My problems are as following: >>1. The format of date is such the numbers like ?"14755" etc. , I have tried >>dat$responsed_at <- as.Date ( dat$responsed_at,format="%Y-%m-%d") >> >>but it looks like doesn't work . In the window of result, the format of date >>is such like "2010-02-02", but in my tables, the format of date has not >>changed. >> >>PS: and i have told you a wrong thing, now my date format should still be >>"%Y-%m-%d". No need to change into "%d-%m-%Y" >> >>2. How can i merge the two dataframe into one? >>as you have mentioned in your reponse, your code is >>res<-merge(datNew,dat2,all=TRUE,by="responsed_at") >>I've tried that, but the error is : >> >>Error in charToDate(x) : >>? character string is not in a standard unambiguous format >> >>I think that is also the problem of the format of date, isn't that? >> >>3. because for the patient concerned,in the original database, there are >>only the number of connection, which i ?want is to see if ?the patient had >>responsed the questionnaire every month. So the 'id' should be conserved >>too. >> >>Thank you so much. >></quote> >>Quoted from: >>http://r.789695.n4.nabble.com/how-to-change-the-date-into-an-interval-of-date-tp4664071p4664339.html >> >> >>_____________________________________ >>Sent from http://r.789695.n4.nabble.com >> >> > > >
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 <guanguanluo at gmail.com> To: arun <smartpink111 at 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 <smartpink111 at 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. > > > >
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 <guanguanluo at gmail.com> To: arun <smartpink111 at 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.
Hi, Try this: library(mondate) mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],"-01",sep="")))),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 8 #7?????????? 1?????? 2011-2 9 #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 If it change: evt_c.1$responsed_at[6:7]<- c("2011-05","2011-07") ?mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],"-01",sep="")))),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-05 12 #7?????????? 1????? 2011-07 14 #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 <guanguanluo at gmail.com> To: arun <smartpink111 at yahoo.com> Sent: Wednesday, April 17, 2013 9:25 AM Subject: Re: how to change the date into an interval of date? 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 8 7 1 2011-2 9 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 the order i want. you are so kind-hearted. GG 2013/4/17 arun <smartpink111 at yahoo.com> Alright, Sorry, I misunderstood.? So, what do you want your result to be at "2011-1".? Is it 0?> > > > > > >________________________________ > From: GUANGUAN LUO <guanguanluo at gmail.com> >To: arun <smartpink111 at 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 t1 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. > > > > >2013/4/17 arun <smartpink111 at yahoo.com> > >If this is not what your problem, please provide a dataset like below and explain where is the problem? >> >> >> >> >> >>----- Original Message ----- >>From: arun <smartpink111 at yahoo.com> >>To: GUANGUAN LUO <guanguanluo at gmail.com> >>Cc: >>Sent: Wednesday, April 17, 2013 9:17 AM >>Subject: Re: how to change the date into an interval of date? >> >>Hi, >>I am not sure I understand your question: >>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 >>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.1,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?????????? 2?????? 2010-5 0 >>6?????????? 2?????? 2010-6 1 >>7?????????? 2?????? 2010-7 2 >>8?????????? 3?????? 2010-1 0 >>9?????????? 3?????? 2010-2 1 >>10????????? 3?????? 2010-4 3 >>11????????? 3?????? 2010-5 4 >>12????????? 4????? 2011-01 0 >>13????????? 4????? 2011-03 2 >>14????????? 5????? 2012-04 0 >>15????????? 5????? 2012-06 2 >>A.K. >> >> >> >>________________________________ >>From: GUANGUAN LUO <guanguanluo at gmail.com> >>To: arun <smartpink111 at yahoo.com> >>Sent: Wednesday, April 17, 2013 9:11 AM >>Subject: Re: how to change the date into an interval of date? >> >> >> >>but the date is untill 2012, if you do like this, t of 2011-01 would be -4. >> >> >>2013/4/17 GUANGUAN LUO <guanguanluo at gmail.com> >> >>but the date is untill 2012, if you do like this, t of 2011-01 would be -1. >>> >>> >>> >>>2013/4/17 arun <smartpink111 at yahoo.com> >>> >>>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 <guanguanluo at gmail.com> >>>>To: arun <smartpink111 at 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 <smartpink111 at 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. >>>>> >>>>> >>>>> >>>>> >>>> >>>> >>> >> >> > > >
Hi. No problem. cc:ing to Rhelp. A.K. ________________________________ From: GUANGUAN LUO <guanguanluo@gmail.com> Sent: Wednesday, April 17, 2013 10:25 AM Subject: Re: how to change the date into an interval of date? Thank you so much . That is exactly the things i want. GG Hi,>Try this: >library(mondate) >mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],"-01",sep="")))),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 8 >#7 1 2011-2 9 >#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 >If it change: >evt_c.1$responsed_at[6:7]<- c("2011-05","2011-07") > mutate(evt_c.1,t=ave(round(as.numeric(mondate(paste(evt_c.1[,2],"-01",sep="")))),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-05 12 >#7 1 2011-07 14 > >#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 <guanguanluo@gmail.com>>Sent: Wednesday, April 17, 2013 9:25 AM > >Subject: Re: how to change the date into an interval of date? > > > >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 8 >7 1 2011-2 9 >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 the order i want. you are so kind-hearted. > >GG >> >Alright, Sorry, I misunderstood. So, what do you want your result to be at "2011-1". Is it 0? >> >> >> >> >> >> >>________________________________ >> From: GUANGUAN LUO <guanguanluo@gmail.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. >> >> >> >>>> >>If this is not what your problem, please provide a dataset like below and explain where is the problem? >>> >>> >>> >>> >>> >>>----- Original Message ----->>>To: GUANGUAN LUO <guanguanluo@gmail.com> >>>Cc: >>>Sent: Wednesday, April 17, 2013 9:17 AM >>>Subject: Re: how to change the date into an interval of date? >>> >>>Hi, >>>I am not sure I understand your question: >>>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 >>>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.1,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 2 2010-5 0 >>>6 2 2010-6 1 >>>7 2 2010-7 2 >>>8 3 2010-1 0 >>>9 3 2010-2 1 >>>10 3 2010-4 3 >>>11 3 2010-5 4 >>>12 4 2011-01 0 >>>13 4 2011-03 2 >>>14 5 2012-04 0 >>>15 5 2012-06 2 >>>A.K. >>> >>> >>> >>>________________________________ >>>From: GUANGUAN LUO <guanguanluo@gmail.com>>>>Sent: Wednesday, April 17, 2013 9:11 AM >>>Subject: Re: how to change the date into an interval of date? >>> >>> >>> >>>but the date is untill 2012, if you do like this, t of 2011-01 would be -4. >>> >>> >>>2013/4/17 GUANGUAN LUO <guanguanluo@gmail.com> >>> >>>but the date is untill 2012, if you do like this, t of 2011-01 would be -1. >>>> >>>> >>>>>>>> >>>>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 <guanguanluo@gmail.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 >>>>> >>>>> >>>>> >>>>>>>>>> >>>>>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. >>>>>> >>>>>> >>>>>> >>>>>> >>>>> >>>>> >>>> >>> >>> >> >> >> >[[alternative HTML version deleted]]