[英]How to convert 24-hour numeric values to time using lubridate? (R)
library(lubridate)
df <- tibble(var1 = c(0, 30, 830, 1400, NA), var2 = c("A", "A", "A", "A", "A"))
我有一組代表 24 小時的數值,例如,0 = 0:00、30 = 0;30、830 = 8:30、1400 = 14:00。 我想將這些值轉換為lubridate
中的時間字段。 但是,當我嘗試使用hm(df)
時收到以下消息
Warning message:
In .parse_hms(..., order = "HM", quiet = quiet) :
Some strings failed to parse, or all strings are NAs
首先添加可變數量的前導零,然后轉換為 posixct,最后只提取小時和分鍾。
strftime(strptime(sapply(paste0("0000", df), function(i) substring(i, nchar(i) - 3, nchar(i))), "%H%M"), format = "%H:%M")
00000 000030 0000830 00001400 0000NA
"00:00" "00:30" "08:30" "14:00" NA
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.