I want to find the DOY (Day of Year) of some dates. I think to substract the date 1. January from the data to achive this. Something like:> d <- as.Date("2006-03-13") - as.Date("2006-01-01") +1 > dTime difference of 72 days So far so good. But d is a 'difftime' object. How do I get an Integer value from that? I tried severel things, incuding the following:> dd <- as(d,"numeric")Error in .classEnv(thisClass) : unable to find an environment containing class "difftime" Sugestions appriciated... :-) Martin at Hvidberg.net
Henrique Dallazuanna
2008-May-21 20:09 UTC
[R] Converting a 'difftime' to integer - How to???
Try : as.numeric(d) or also: unclass(d) On Wed, May 21, 2008 at 5:04 PM, Martin Hvidberg <martin@hvidberg.net> wrote:> I want to find the DOY (Day of Year) of some dates. I think to substract > the date 1. January from the data to achive this. Something like: > > > d <- as.Date("2006-03-13") - as.Date("2006-01-01") +1 > > d > Time difference of 72 days > > So far so good. But d is a 'difftime' object. How do I get an Integer value > from that? > > I tried severel things, incuding the following: > > > dd <- as(d,"numeric") > Error in .classEnv(thisClass) : > unable to find an environment containing class "difftime" > > Sugestions appriciated... > > :-) Martin@Hvidberg.net > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Or, convert your object of class Date to POSIXlt, and then use the yday element of the resulting list. Example: today <- Sys.Date() as.POSIXlt(today)$yday Erik Martin Hvidberg wrote:> I want to find the DOY (Day of Year) of some dates. I think to substract the date 1. January from the data to achive this. Something like: > >> d <- as.Date("2006-03-13") - as.Date("2006-01-01") +1 >> d > Time difference of 72 days > > So far so good. But d is a 'difftime' object. How do I get an Integer value from that? > > I tried severel things, incuding the following: > >> dd <- as(d,"numeric") > Error in .classEnv(thisClass) : > unable to find an environment containing class "difftime" > > Sugestions appriciated... > > :-) Martin at Hvidberg.net > > ______________________________________________ > 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.
Martin Hvidberg wrote:> I want to find the DOY (Day of Year) of some dates. I think to substract the date 1. January from the data to achive this. Something like: > > >> d <- as.Date("2006-03-13") - as.Date("2006-01-01") +1 >> d >> > Time difference of 72 days > > So far so good. But d is a 'difftime' object. How do I get an Integer value from that? > > I tried severel things, incuding the following: > > >> dd <- as(d,"numeric") >> > Error in .classEnv(thisClass) : > unable to find an environment containing class "difftime" > > Sugestions appriciated... > >The canonical way is > as.numeric(d, units="days") [1] 72 In fact, you can leave out the units when dealing with differences between Date objects, because it is always "days", but that is an undocumented implementation detail. If you have differences between POSIXt objects, real confusion can arise: > ISOdatetime(2008,1,1,12,0,2) - ISOdatetime(2008,1,1,12,0,0) Time difference of 2 secs > ISOdatetime(2008,1,1,12,2,0) - ISOdatetime(2008,1,1,12,0,0) Time difference of 2 mins > ISOdatetime(2008,1,1,14,0,0) - ISOdatetime(2008,1,1,12,0,0) Time difference of 2 hours -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907