[R] date/time arithmetic

Parlamis Franklin fparlamis at mac.com
Wed Nov 30 21:58:55 CET 2005


I agree there are workarounds (although reduction to secs is not  
necessarily graceful when you want to add, say, "1 month" to a date,  
or any other increment for which the total number of seconds is not  
fixed--and is also complicated by the existence of leap seconds).   
But upon inspection, they are never as clean as they seem.

The representation in the man pages that difftime / date-time  
arithmetic was possible made me hopeful that some of these tasks had  
been handled already, in a manner consistent with the internal date- 
time functions.


On Nov 30, 2005, at 9:42 AM, bogdan romocea wrote:

> What do you need a bunch of functions for? I'm not familiar with the
> details of difftime objects, however an easy way out of here is to get
> the time difference in seconds, which you can then add or subtract as
> you please from date-times.
>
> x<-Sys.time(); y<-Sys.time()+3600
> diff <- as.numeric(difftime(y,x,units="secs"))
> x; y; diff
> y-diff
>
>
>> -----Original Message-----
>> From: r-help-bounces at stat.math.ethz.ch
>> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of
>> Parlamis Franklin
>> Sent: Wednesday, November 30, 2005 1:57 PM
>> To: Parlamis Franklin
>> Cc: r-help at stat.math.ethz.ch
>> Subject: Re: [R] date/time arithmetic
>>
>>
>> Hi.  Sorry to pester, but I didn't get a reply to this
>> (perhaps owing
>> to Thanksgiving break).  This seems to be a case where the R
>> language
>> simply doesn't do what it says it does (i.e., allow addition or
>> subtraction of a difftime object and a date-time object).
>>
>> Am I wrong?  I am about to write a bunch of functions (easy, but
>> perhaps redundant) to do this, and I just want to know whether there
>> is a bug here, or whether I am missing something obvious.
>>
>> Thanks.
>>
>> FP
>>
>> On Nov 23, 2005, at 10:02 AM, Parlamis Franklin wrote:
>>
>>> On the help page "DateTimeClasses {base}" it says:
>>>
>>> "One can add or subtract a number of seconds or a difftime object
>>> from a date-time object, but not add two date-time objects."
>>>
>>> However,
>>>
>>>> x<-Sys.time(); y<-Sys.time()+3600
>>>> diff<-y-x
>>>> x; y; diff
>>> [1] "2005-11-23 19:58:20 GMT"
>>> [1] "2005-11-23 20:58:20 GMT"
>>> Time difference of 1 hours
>>>> y-diff
>>> [1] "2005-11-23 20:58:19 GMT"
>>> Warning message:
>>> Incompatible methods ("-.POSIXt", "Ops.difftime") for "-"
>>>
>>> Do I have the syntax wrong?  Or is some conversion of the difftime
>>> object to raw seconds necessary prior to performing arithmetic?
>>> And is the help page wrong?
>>>
>>> Thanks.
>>>
>>> Franklin
>>>
>>>
>>>
>>>
>>
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>




More information about the R-help mailing list