我知道这个问题以不同的形式(例如here或here)问了很多遍。
但是,我仍然无法将数字/字符串转换为时间。
我的数据集是我导入的一个excel文件,看起来像这样:
df <- structure(list(ID = c(322, 322, 322, 322, 322, 322), Cue = c("F1",
"F1", "F2", "F3", "F4", "F5"), TimeComming = c("1", "1", "1",
"1", "1", "1"), Time = c("0.690210162037037", "0.69030296296296301",
"0.69652906250000002", "0.70100131944444399", "0.70550445601851897",
"0.71043541666666699"), Stime = c("1.0058333333330394E-3", "1.0986342592590459E-3",
"7.3247337962960568E-3", "1.1796990740740032E-2", "1.6300127314815005E-2",
"2.1231087962963024E-2")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -6L), .Names = c("ID", "Cue", "TimeComming",
"Time", "Stime"))
df
# A tibble: 6 x 5
ID Cue TimeComming Time Stime
<dbl> <chr> <chr> <chr> <chr>
1 322. F1 1 0.690210162~ 1.00583333~
2 322. F1 1 0.690302962~ 1.09863425~
3 322. F2 1 0.696529062~ 7.32473379~
4 322. F3 1 0.701001319~ 1.17969907~
5 322. F4 1 0.705504456~ 1.63001273~
6 322. F5 1 0.710435416~ 2.12310879~
所需的输出应如下所示:
ID Cue TimeComming Time Stime
1 322 F1 1 16:33:54 00:01:26.904
2 322 F1 1 16:34:02 00:01:34.922
3 322 F2 1 16:43:00 00:10:32.857
4 322 F3 1 16:49:27 00:16:59.260
5 322 F4 1 16:55:56 00:23:28.331
6 322 F5 1 17:03:02 00:30:34.366
答案 0 :(得分:2)
一点点算术就能为您生成碎片:
time_since_midnight <- function(x, decimal_second = FALSE){
hours <- x %/% 60
minutes <- x %% 60
seconds_since_last_hour <- minutes * 60
minutes <- seconds_since_last_hour %/% 60
seconds <- seconds_since_last_hour %% 60
if (decimal_second){
sprintf("%02d:%02d:%02.3f", hours, minutes, seconds)
} else {
sprintf("%02d:%02d:%02d", hours, minutes, floor(seconds))
}
}
df$Time <- time_since_midnight(as.numeric(df$Time) * 24 * 60)
df$Stime <- time_since_midnight(as.numeric(df$Stime) * 24*60,
decimal_second = TRUE)
与其他建议的解决方案相比,处理时间没有太大差异:
df <- do.call("rbind", replicate(1000, df, simplify = FALSE))
library(microbenchmark)
library(lubridate)
library(data.table)
microbenchmark(
posix =
{
format(as.POSIXct(24 * 3600 * as.numeric(df$Time), origin = "2018-01-01", tz = "UTC"),
format = "%H:%M:%S")
},
arith =
{
time_since_midnight(as.numeric(df$Time) * 24 * 60)
},
lubridate =
{
round(seconds_to_period(as.numeric(df$Stime) * 86400), 3)
},
data.table =
{
data.table::IDateTime(24 * 3600 * as.numeric(df$Time))$itime
}
)
Unit: milliseconds
expr min lq mean median uq max neval cld
posix 15.431598 15.611066 16.166188 15.737016 16.127330 20.280457 100 c
arith 6.959072 7.006285 7.293416 7.057017 7.133262 13.945123 100 b
lubridate 4.476727 4.728041 6.464536 5.749277 6.044285 60.472758 100 b
data.table 1.329881 1.412430 1.529952 1.490874 1.557735 2.716651 100 a
答案 1 :(得分:1)
library(lubridate)
round(seconds_to_period(as.numeric(substr(df1$Time, 1, 20))*86400))
OR
round(seconds_to_period(as.numeric(gsub("~", "", df1$Time))*86400))
摆脱〜。两者都给出:
[1] "16H 33M 54S" "16H 34M 2S" "16H 43M 0S" "16H 49M 27S" "16H 55M 56S" "17H 3M 2S"
对于Stime:
round(seconds_to_period(as.numeric(gsub("~", "", df1$Stime)) * 86400), 3)
[1] "1M 26.904S" "1M 34.922S" "10M 32.857S" "16M 59.26S" "23M 28.331S" "30M 34.366S"