Re: [R] Problem with diff(strptime(...

From: Jim Lemon <>
Date: Thu, 20 Mar 2008 23:53:01 +1100

Prof Brian Ripley wrote:
> You are throwing away the clue in your use of as.numeric.
> First. strptime returns a POSIXlt value, which you will convert to
> POSIXct when you do arithetic (using diff()). Why are you doing that? So

>> foodate<-factor(c("1/7/1991","1/8/1991","1/8/1991","3/8/1991"))
>> diff(strptime(foodate,"%d/%m/%Y"))

> Time differences in secs
> [1] 2678400 0 172800
> attr(,"tzone")
> [1] ""
> is correct. I think you intended
> diff(as.Date(foodate,"%d/%m/%Y"))/365.25
> or even add as.numeric() inside diff().
This is true, but I am puzzled as to why I get the correct output except when there are two consecutive input values that are the same. The idea was to get the number of years between each date in order to calculate a   rate per year. If I put the as.numeric inside diff:

diff(as.numeric(strptime(foodate,"%d/%m/%Y"))/365.25) Error in Ops.POSIXt(as.numeric(strptime(foodate, "%d/%m/%Y")), 365.25) :

   / not defined for "POSIXt" objects

Jim mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu 20 Mar 2008 - 12:53:13 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 20 Mar 2008 - 13:30:23 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive