I have columns in the following way
The first column has year month day together. I want to combine columns 1, 4, 5 into DateTime format in R. Please help me. I have used as.date but getting error.
I have columns in the following way
The first column has year month day together. I want to combine columns 1, 4, 5 into DateTime format in R. Please help me. I have used as.date but getting error.
The following function combines 3 columns into one object of class "POSIXct"
.
yyyymmdd
;The seconds are set to zero.
toDateTime <- function(x){
d <- as.Date(x[[1]], format = "%Y%m%d")
ISOdatetime(format(d, "%Y"), format(d, "%m"), format(d, "%d"),
hour = x[[2]], min = x[[3]], sec = 0L)
}
toDateTime(df1[c(1, 4, 5)])
# [1] "2001-01-01 01:00:00 WET" "2001-01-01 01:30:00 WET"
# [3] "2001-01-01 02:00:00 WET" "2001-01-01 02:30:00 WET"
# [5] "2001-01-01 03:00:00 WET" "2001-01-01 03:30:00 WET"
# [7] "2001-01-01 04:00:00 WET" "2001-01-01 04:30:00 WET"
# [9] "2001-01-01 05:00:00 WET" "2001-01-01 05:30:00 WET"
#[11] "2001-01-01 06:00:00 WET" "2001-01-01 06:30:00 WET"
#[13] "2001-01-01 07:00:00 WET" "2001-01-01 07:30:00 WET"
Test data
In the test data that follows I have set columns and 3 to NA
, since they are not used.
dates <- "20010101"
hour <- rep(1:7, each = 2)
min <- rep(c(0, 30), length.out = 14)
df1 <- data.frame(dates, times = NA, d = NA, hour, min)
For the sake of completeness, here is a different approach which uses
sprintf()
to create a vector of character datetimes which is then converted byas.POSIXct()
.with(df1, )
is used instead of typingdf1$dates
,df1$hour
,df1$min
.Also note that the timezone has been set explicitely.
Another alternative is to use the
anytime
package:Note that no format specifiers are required. Only a blank needs to be inserted between
dates
andhour
.And finally, the
lubridate
package can be used:Data