Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert hours since date to datetime format in r

Tags:

r

I've downloaded some climate data from a website and am trying to analyse it in R.

The time format for the data is hours since 1800-01-01 00:00. For example:

ss <- seq(447042,455802, length.out = 1461)

which shows data at 6 hour intervals.

How can I convert this to a an actual time in R. This example should give data for 1851:

1851-01-01 00:00
1851-01-01 06:00

and so on...

How can this be done?

Any advice would be appreciated.

like image 727
Emma Tebbs Avatar asked Jun 11 '15 13:06

Emma Tebbs


People also ask

How do I convert CHR to time in R?

We can convert the character to timestamp by using strptime() method. strptime() function in R Language is used to parse the given representation of date and time with the given template.

What is POSIXct format in R?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.


1 Answers

I think you have a typo as well as incorrect calculations. Let's assume you want time in the future of 1880 rather than the past. So it might be 1951 you wanted? Then to convert hours to seconds which are the basis for the POSIXt classed objects, just multiply by 3600 = 60*60:

> tail( as.POSIXct(ss*3600,origin='1880-01-01 00:00') )
[1] "1931-12-30 04:00:00 PST" "1931-12-30 10:00:00 PST" "1931-12-30 16:00:00 PST"
[4] "1931-12-30 22:00:00 PST" "1931-12-31 04:00:00 PST" "1931-12-31 10:00:00 PST"

As you can see it's nowhere the year 1951 either, but maybe you had two digits off and you wanted 1931? Conversions that span the range of years before the onset of daylight savings time and cross century boundaries where leap years and leap seconds were used may not "line up" with your expectations.

like image 169
IRTFM Avatar answered Sep 30 '22 09:09

IRTFM