在非常大的数据框中,我的时间以以下minutes:seconds格式显示:
Time Period
6:21 1
8:52 2
15:00 2
...
如何做到这一点,以便新的时间列仅读取时间列中的总秒数?
Time Period Seconds
6:21 1 381
8:52 2 532
15:00 2 900
进一步,我如何使Seconds变量乘以Period变量(以显示经过的总秒数)?
Time Period Seconds TimePassed
6:21 1 381 381
8:52 2 532 1064
15:00 2 900 1800
答案 0 :(得分:-1)
使用period_to_seconds
将“时间”列转换为Period
类后,这里是ms
的一个选项。然后,我们将“ Seconds”和“ Period”相乘以创建“ TimePassed”
library(lubridate)
df1$Seconds <- period_to_seconds(ms(df1$Time))
df1$TimePassed <- df1$Seconds * df1$Period
-输出
df1
# Time Period Seconds TimePassed
#1 6:21 1 381 381
#2 8:52 2 532 1064
#3 15:00 2 900 1800
df1 <- structure(list(Time = c("6:21", "8:52", "15:00"), Period = c(1L,
2L, 2L)), class = "data.frame", row.names = c(NA, -3L))