![](/img/trans.png)
[英]How to split a date and time column into separate day, month, hour, minute, second, day of week columns in r?
[英]format day hour minute second column in r
我有一個列名預期的數據框:
df = data.frame(anticipation = c('1d0s','1d5h49m6s','5h39m','8h9s')
我想將預期列排列為%d %h %m %s
格式。 預期的 output 如下:
output
1d 0h 0m 0s
1d 5h 49m 6s
0d 5h 39m 0s
0d 8h 0m 9s
我嘗試過像這樣手動執行此操作:
df$Anticipation.day = as.numeric(as.character(data.frame(str_split_fixed(df$anticipation, "d", 2))$X1))
and so on
但它變得非常復雜。 任何幫助將不勝感激。 TIA。
time2num <- function(x, mult = c(24*60*60, 60*60, 60, 1)) {
vapply(strsplit(x, "\\D+"), function(z) sum(as.numeric(z) * tail(mult, n = length(z))), numeric(1))
}
vec <- c("1d 5h 49m 6s", "0d 5h 39m 0s")
time2num(vec)
# [1] 107346 20340
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.