R, creating time objects from the date and hour (integer)

I have data presented in the form of a date indicating the day (format "YYYY-MM-DD", for example, "2015-03-11", and the hours of the day, numbered (0-23).

What is the most convenient way to create temporary form objects

"2015-03-11" and hour = 0 -> "2015-03-11 00:00" "2015-03-11" and hour = 1 -> "2015-03-11 01:00" "2015-03-11" and hour = 2 -> "2015-03-11 02:00" 

I could use the Date function from Base or something from xts or timeDate. It should be easy, but I'm sure someone out there knows this quickly.

EDIT: data is provided in 2 columns, one for the date and one numerical.

+7
datetime r date-conversion posixct
source share
4 answers

Suppose we have this input:

 date <- c("2015-03-11", "2015-03-12") hour <- 2:3 

then try one of them:

1) chron

 library(chron) as.chron(date) + hour/24 

giving:

 [1] (03/11/15 02:00:00) (03/12/15 03:00:00) 

2) POSIXct . In this case, only the R base is used, there are no packages:

 as.POSIXct(date) + 3600 * hour 

on my system:

 [1] "2015-03-11 02:00:00 EDT" "2015-03-12 03:00:00 EDT" 

If you want to get the result in the UTC time zone, use:

 as.POSIXct(date, tz = "UTC") + 3600 * hour 

3) lubridate

 library(lubridate) ymd(date) + hours(hour) 

giving:

 [1] "2015-03-11 02:00:00 UTC" "2015-03-12 03:00:00 UTC" 

If you want in the current time zone, then:

 ymd(date, tz = "") + hours(hour) 

Note that the chron solution provides a date / time class that does not use time zones, eliminating many of the problems that time zones can cause. POSIXct and lubridate solutions give the date / time in a specific time zone, as shown.

+4
source share

You do not need an external package for this.
If your data is in this format:

 df=data.frame(date=c("2015-03-11","2015-03-11","2015-03-11"),hour=0:2) 

just apply the following function:

 format(as.POSIXct(df$date)+df$hour*60*60, format = "%Y-%m-%d %H:%M") 
+5
source share

You can try

 dtime <- with(df, as.POSIXct(sprintf('%s %02d', date, hour), format = "%Y-%m-%d %H")) 

and then use format , as in other posts

or

  library(lubridate) ymd_h(with(df, sprintf('%s %02d', date, hour))) 

Or a little more compact

 ymd_h(do.call(paste, df)) 
+3
source share

Try it. You can format it without seconds using format if you want, although I think it is preferable to save it in the POSIXct class POSIXct that you can manipulate it with afterwords (adding deletion of days, seconds, etc.).

 as.POSIXct(do.call(paste, df), format = "%Y-%m-%d %H") ## [1] "2015-03-11 00:00:00 IST" "2015-03-11 01:00:00 IST" "2015-03-11 02:00:00 IST" 

Although, if you insist on your exact output, here is a solution with format

 format(as.POSIXct(do.call(paste, df), format = "%Y-%m-%d %H"), "%Y-%m-%d %H:%M") ## [1] "2015-03-11 00:00" "2015-03-11 01:00" "2015-03-11 02:00" 

Data

 df <- structure(list(V1 = structure(c(1L, 1L, 1L), .Label = "2015-03-11", class = "factor"), V2 = 0:2), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -3L)) 
+2
source share

All Articles