【问题标题】:Want to combine the following into datetime column in R想要将以下内容组合到 R 中的日期时间列中
【发布时间】:2020-10-04 17:55:46
【问题描述】:

我有以下方式的列

date, hour minute column

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

【问题讨论】:

  • 请提供最少且可重现的示例以及所需的输出。使用dput() 分享您的部分数据。

标签: r as.date


【解决方案1】:

以下函数将 3 列组合成一个 "POSIXct" 类的对象。

  1. yyyymmdd 格式的年月日组合列;
  2. 一小时专栏;
  3. 一分钟专栏。

秒数设置为零。

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)

【讨论】:

    【解决方案2】:

    为了完整起见,这里有一种不同的方法,它使用sprintf() 创建一个字符日期时间向量,然后由as.POSIXct() 转换。使用with(df1, ) 代替输入df1$dates、df1$hour、df1$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"
    

    请注意,不需要格式说明符。只需在dates 和hour 之间插入一个空格。

    最后,可以使用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))
    

    【讨论】:

    • 非常感谢。它运行良好,感谢您分享从创建新数据框到解决方案的过程。
    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2022-06-28
    • 2020-04-16
    • 2013-01-10
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    相关资源
    最近更新 更多