How to convert a Unix timestamp to POSIXct in R (i.e. 1532289300 = 2018-07-22 19:55:00)
If you are converting a column of Unix timestamps in a dataframe:
> df$Your.Times <- as.POSIXct(df$Your.Times, origin = “1970-1-1”) |
… or …
If you are converting a single Unix timestamp:
> yourtime = as.POSIXct(1532289300, origin = “1970-1-1”) |
Note: These solutions require the lubridate package.
Explanation:
In brief, as understood by Unix, time started on January 1, 1970 (the epoch date). The epoch date is represented as “0” Coordinated Universal Time (“UTC”) in Unix systems and all datetimes after the epoch date are represented as the number of seconds that have passed since the epoch date. Therefore, a Unix timestamp is the number of seconds since January 1, 1970 … approximately … leap seconds are not counted. as.POSIXct works be converting the number of seconds since January 1, 1970 into a readable datetime.