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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With