[R] how to change the date into an interval of date?
arun
smartpink111 at yahoo.com
Wed Apr 17 16:11:19 CEST 2013
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 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.
>
>
>
>
>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.
>>>>>
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>>
>
>
>
More information about the R-help
mailing list