【问题标题】:How to Convert time in minutes:seconds format to just seconds? [duplicate]如何将时间以分钟为单位:秒格式转换为秒? [复制]
【发布时间】:2021-01-28 23:57:03
【问题描述】:

在我非常大的数据框中,我有以下分钟:秒格式的时间:

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

【问题讨论】:

  • df$newtime

标签: r dataframe


【解决方案1】:

在使用ms 将时间列转换为Period 类之后,这是一个带有period_to_seconds 的选项。然后,我们将 '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))

【讨论】:

    猜你喜欢
    • 2013-11-11
    • 2020-10-16
    • 2020-09-04
    • 2014-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多