[R] difference in dates

Erin Hodgess erinm.hodgess at gmail.com
Mon Jun 21 02:29:07 CEST 2010


Thanks for the help!





> a.df[1:10,c(1,6,9)]
           DATE      DEATH  DEATH1
1207 2009-04-16 2009-05-06 20 days
1514 2009-04-16 2009-05-06 20 days
2548 2009-04-16 2009-05-08 22 days
3430 2009-04-16 2009-05-09 23 days
3851 2009-04-16 2009-05-09 23 days
3945 2009-04-16 2009-05-09 23 days
7274 2009-04-16 2009-05-12 26 days
7532 2009-04-16 2009-05-12 26 days
7651 2009-04-16 2009-05-12 26 days
8495 2009-04-16 2009-05-13 27 days
>


2010/6/20 Christos Argyropoulos <argchris at hotmail.com>:
> Hi,
> Are you sure these are date objects and not strings? For example
>
>> d1<-runif(10,0,100)
>> d2<-runif(10,0,200)
>> df<-data.frame(d1=d1+as.Date("2010-6-20")+d1,d2=as.Date("2009-6-20")+d2)
>> df
>            d1         d2
> 1  2010-09-23 2009-06-30
> 2  2010-06-25 2009-08-21
> 3  2010-10-10 2009-08-04
> 4  2010-07-06 2009-07-30
> 5  2010-07-05 2009-10-19
> 6  2010-07-07 2009-08-01
> 7  2010-11-09 2009-09-25
> 8  2010-07-19 2009-12-03
> 9  2010-12-24 2009-07-19
> 10 2010-09-25 2009-06-27
>> df$DD<-df$d1-df$d2
>> df
>            d1         d2            DD
> 1  2010-09-23 2009-06-30 449.7866 days
> 2  2010-06-25 2009-08-21 307.0807 days
> 3  2010-10-10 2009-08-04 431.7428 days
> 4  2010-07-06 2009-07-30 341.2171 days
> 5  2010-07-05 2009-10-19 259.0490 days
> 6  2010-07-07 2009-08-01 339.7433 days
> 7  2010-11-09 2009-09-25 410.1609 days
> 8  2010-07-19 2009-12-03 227.1901 days
> 9  2010-12-24 2009-07-19 523.5030 days
> 10 2010-09-25 2009-06-27 454.6529 days
>
> generates differences between the 2 columns without NAs.
> What's the output you get when you call str on your data frame?
>
> Christos
>
>
>> Date: Sun, 20 Jun 2010 19:13:24 -0500
>> From: erinm.hodgess at gmail.com
>> To: r-help at stat.math.ethz.ch
>> Subject: [R] difference in dates
>>
>> Dear R People:
>>
>> I have a data frame with the two following date columns:
>>
>>
>> > a.df[1:10,c(1,6)]
>> DATE DEATH
>> 1207 2009-04-16 2009-05-06
>> 1514 2009-04-16 2009-05-06
>> 2548 2009-04-16 2009-05-08
>> 3430 2009-04-16 2009-05-09
>> 3851 2009-04-16 2009-05-09
>> 3945 2009-04-16 2009-05-09
>> 7274 2009-04-16 2009-05-12
>> 7532 2009-04-16 2009-05-12
>> 7651 2009-04-16 2009-05-12
>> 8495 2009-04-16 2009-05-13
>> >
>>
>> I would like to generate a 3rd column which is the difference in days
>> between the two dates.
>>
>> I tried
>>
>> a.df$DEATH - a.df$DATE
>>
>> but I obtain all NA.
>>
>> Any help would be much appreciated.
>>
>> Thanks,
>> Erin
>>
>>
>>
>> --
>> Erin Hodgess
>> Associate Professor
>> Department of Computer and Mathematical Sciences
>> University of Houston - Downtown
>> mailto: erinm.hodgess at gmail.com
>>
>> ______________________________________________
>> R-help at 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.
>
> ________________________________
> Hotmail: Powerful Free email with security by Microsoft. Get it now.



-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodgess at gmail.com



More information about the R-help mailing list