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
Are you sure your data was the Date class?> x <- read.table('clipboard', header=TRUE) > xDATE 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 > xDATE 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?
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) > dfd1 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 > dfd1 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@gmail.com > To: r-help@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@gmail.com > > ______________________________________________ > R-help@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. [[alternative HTML version deleted]]