想要将以下内容合并到 R 中的日期时间列中

Want to combine the following into datetime column in R

我有以下方式的专栏

date, hour minute column

第一列年月日在一起。 我想将第 1、4、5 列合并为 R 中的 DateTime 格式。请帮助我。我已经使用 as.date 但出现错误。

以下函数将class"POSIXct".

的3列合并为一个对象
  1. yyyymmdd;
  2. 格式组合年月日的列
  3. 一小时专栏;
  4. 一分钟专栏。

秒设置为零。

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"

测试数据

在后面的测试数据中,我将列和 3 设置为 NA,因为它们未被使用。

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)

为了完整起见,这里有一个不同的方法,它使用 sprintf() 创建一个字符日期时间向量,然后由 as.POSIXct() 转换。使用 with(df1, ) 而不是键入 df1$datesdf1$hourdf1$min.

with(df1, as.POSIXct(sprintf("%s%02i%02i", dates, hour, min), tz = "UTC", format = "%Y%m%d%H%M"))
 [1] "2001-01-01 01:00:00 UTC" "2001-01-01 01:30:00 UTC" "2001-01-01 02:00:00 UTC"
 [4] "2001-01-01 02:30:00 UTC" "2001-01-01 03:00:00 UTC" "2001-01-01 03:30:00 UTC"
 [7] "2001-01-01 04:00:00 UTC" "2001-01-01 04:30:00 UTC" "2001-01-01 05:00:00 UTC"
[10] "2001-01-01 05:30:00 UTC" "2001-01-01 06:00:00 UTC" "2001-01-01 06:30:00 UTC"
[13] "2001-01-01 07:00:00 UTC" "2001-01-01 07:30:00 UTC"

另请注意,时区已明确设置。

另一种选择是使用 anytime 包:

with(df1, anytime::utctime(sprintf("%s %02i%02i", dates, hour, min), tz = "UTC"))
 [1] "2001-01-01 01:00:00 UTC" "2001-01-01 01:30:00 UTC" "2001-01-01 02:00:00 UTC"
 [4] "2001-01-01 02:30:00 UTC" "2001-01-01 03:00:00 UTC" "2001-01-01 03:30:00 UTC"
 [7] "2001-01-01 04:00:00 UTC" "2001-01-01 04:30:00 UTC" "2001-01-01 05:00:00 UTC"
[10] "2001-01-01 05:30:00 UTC" "2001-01-01 06:00:00 UTC" "2001-01-01 06:30:00 UTC"
[13] "2001-01-01 07:00:00 UTC" "2001-01-01 07:30:00 UTC"

请注意,不需要格式说明符。 dateshour之间只需要插入一个空格即可。

最后,可以使用lubridate包:

with(df1, lubridate::ymd_hm(sprintf("%s%02i%02i", dates, hour, min)))
 [1] "2001-01-01 01:00:00 UTC" "2001-01-01 01:30:00 UTC" "2001-01-01 02:00:00 UTC"
 [4] "2001-01-01 02:30:00 UTC" "2001-01-01 03:00:00 UTC" "2001-01-01 03:30:00 UTC"
 [7] "2001-01-01 04:00:00 UTC" "2001-01-01 04:30:00 UTC" "2001-01-01 05:00:00 UTC"
[10] "2001-01-01 05:30:00 UTC" "2001-01-01 06:00:00 UTC" "2001-01-01 06:30:00 UTC"
[13] "2001-01-01 07:00:00 UTC" "2001-01-01 07:30:00 UTC"

数据

df1 <- data.frame(dates = rep("20010101", 14L), 
                  times = NA, d = NA, 
                  hour = rep(1:7, each = 2L), 
                  min = rep(c(0L, 30L), 7L))