[R] difference in dates

jim holtman jholtman at gmail.com
Mon Jun 21 02:20:39 CEST 2010


Are you sure your data was the Date class?

> x <- read.table('clipboard', header=TRUE)
> x
           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
> ?as.Date
> x$DATE <- as.Date(x$DATE)
> x$DEATH <- as.Date(x$DEATH)
> x$length <- x$DEATH - x$DATE
> x
           DATE      DEATH  length
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


On Sun, Jun 20, 2010 at 8:13 PM, Erin Hodgess <erinm.hodgess at gmail.com> wrote:
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list