舊工具包生成一個 DateTime 列,格式為:
"d-HH:MM:SS" 例如 "26-16:24:40" 應該是 "2021-11-26 16:24:40"
我在問是否有更優雅的方法來轉換此列?
我的嘗試:
dat %>%
mutate(year = "2021", month = "11") %>%
mutate(day = substr(date.time, 1, 2),
hour = substr(date.time, 4, 5),
minute = substr(date.time, 7, 8),
second = substr(date.time, 10, 11)) %>%
mutate(date = make_datetime(year, month, day, hour, minute, second))
我很好奇看到其他方法。下面的一些示例日期。
testDate <- c("26-16:24:40", "26-16:29:40", "26-16:34:40", "26-16:39:40", "26-16:44:40", "26-16:49:40", "26-16:54:40", "26-16:59:40", "26-17:04:40", "26-17:09:40")
uj5u.com熱心網友回復:
as.POSIXct(paste0("2021-11-", testDate), format = "%Y-%m-%d-%H:%M:%S")
[1] "2021-11-26 16:24:40 CET" "2021-11-26 16:29:40 CET" "2021-11-26 16:34:40 CET" "2021-11-26 16:39:40 CET"
[5] "2021-11-26 16:44:40 CET" "2021-11-26 16:49:40 CET" "2021-11-26 16:54:40 CET" "2021-11-26 16:59:40 CET"
[9] "2021-11-26 17:04:40 CET" "2021-11-26 17:09:40 CET"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/370515.html
上一篇:Python:生成每周時間戳
