[R] Converting a 'difftime' to integer - How to???

Erik Iverson iverson at biostat.wisc.edu
Wed May 21 22:13:47 CEST 2008


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.



More information about the R-help mailing list