Andrew
Andrew

Reputation: 45

R POSIXct origin weird behavior

I have a problem with as.POSIXct(). I need to process a lot of data and in order to minimize memory used for dates I want to change origin. So, I use:

as.integer(as.POSIXct("2015-05-15 00:00:01",origin=("2015-05-15 00:00:00")))

and get 1431637201 when I thought I should get 1. The questions is how to handle origin?

Upvotes: 1

Views: 756

Answers (1)

RHertel
RHertel

Reputation: 23818

The origin parameter is not suitable for this purprose.

Try

as.integer(as.POSIXct("2015-05-15 00:00:01") - as.POSIXct("2015-05-15 00:00:00"))
#[1] 1

or

as.integer(difftime("2015-05-15 00:00:01", "2015-05-15 00:00:00"))

The origin parameter is used to convert a number to a date / date-time object. The number represents units of time (in seconds) passed since an "epoch". The standard epoch for the POSIX* class in R is "1970-01-01". Other systems use other default origins. When using numbers that represent date/time objects that originate from other systems (like SAS or Excel), the "origin" needs to be changed from the "usual" "1970-01-01" and adapted according to the definition of the source from which the numbers have been supplied in order to reproduce the correct date/time in R.

The "origin" option is ignored if a date instead of a number is supplied as the first parameter.

The number obtained in the example shown in the OP depends on the timezone, and it can be reproduced in this case like this:

as.POSIXct(1431637201, origin="1970-01-01", tz="CEST-3")
#[1] "2015-05-15 00:00:01 CEST"

See ?as.POSIXct and ?base::as.Date for more information and examples concerning the use of origin.

Upvotes: 4

Related Questions