在我的csv文件中,我有一个格式为“0 days 00:00:17.740000000”的字符列,表示持续时间。我怎么能用dplyr(post_files
)作为持续时间阅读,或者在用lubridate阅读后进行转换?
答案 0 :(得分:2)
假设所有日期都具有相似的结构。 模拟数据:
set.seed(12345)
days <- sample(0:20, 10, replace = T)
hours <- sample(0:24, 10, replace = T)
mins <- sample(0:60, 10, replace = T)
secs <- sample(0:60, 10, replace = T) + runif(10, 0, 1)
DF <- data.frame(date = sprintf('%s days %s:%s:%s', days, hours, mins, secs))
然后我们分开几天&#34;手工&#34;,转换类型并将所有内容重新组合成period
:
library('tidyr')
library('lubridate')
library('dplyr')
DF %>%
separate(date, c('days', 'time'), sep = ' days ') %>%
mutate(elapsed = period(days = as.numeric(days)) + hms(time))
days time elapsed
1 15 00:27:48.782193281 15d 0H 27M 48.782193281S
2 18 03:19:0.429198820 18d 3H 19M 0.42919882S
3 15 18:58:11.927273975 15d 18H 58M 11.927273975S
4 18 00:43:41.773243225 18d 0H 43M 41.773243225S
5 9 09:39:22.259681247 9d 9H 39M 22.259681247S
6 3 11:23:22.321224673 3d 11H 23M 22.321224673S
7 6 09:42:52.060195157 6d 9H 42M 52.060195157S
8 10 10:33:55.043456454 10d 10H 33M 55.043456454S
9 15 04:13:37.055053818 15d 4H 13M 37.055053818S
10 20 23:29:8.625542797 20d 23H 29M 8.625542797S