Quantcast

difference in dates

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

difference in dates

Erin Hodgess-2
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: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: difference in dates

jholtman
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 <[hidden email]> 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: [hidden email]
>
> ______________________________________________
> [hidden email] 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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: difference in dates

Christos Argyropoulos
In reply to this post by Erin Hodgess-2

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: [hidden email]
> To: [hidden email]
> 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: [hidden email]
>
> ______________________________________________
> [hidden email] 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: difference in dates

Erin Hodgess-2
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 <[hidden email]>:

> 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: [hidden email]
>> To: [hidden email]
>> 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: [hidden email]
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email]

______________________________________________
[hidden email] 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.
Loading...