[R] how to change the date into an interval of date?

arun smartpink111 at yahoo.com
Fri Apr 12 21:48:10 CEST 2013


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 the 
first day to the last day of one month count one number of month. 

>Thank you in avance.



More information about the R-help mailing list