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

Jim Lemon jim at bitwrit.com.au
Thu Mar 20 13:53:01 CET 2008


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



More information about the R-help mailing list