【问题标题】:Convert numeric/string to time in r from imported excel从导入的 excel 将数字/字符串转换为 r 中的时间
【发布时间】:2018-06-26 11:17:57
【问题描述】:

我知道这个问题以不同的形式被问过很多次(例如,herehere)。

但是,我仍然无法将数字/字符串转换为时间。

我的数据集是一个我导入的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

【问题讨论】:

  • 这是什么格式?
  • 在 excel Time 在格式单元格中定义为时间类型 13:30:55,时间 - 自定义 - 类型 hh:mm:ss.000
  • Time 字段似乎是 24 小时的一小部分,而 Stime 字段以分钟为单位
  • 你不能在 Excel 中将列定义为“文本”,以避免在从 Excel 写入时出现有趣的 Excel 转换吗?
  • as.POSIXct(24 * 3600 * 0.690210162, origin = "2018-01-01", tz = "UTC")。然后用format来抢时间部分。

标签: r excel time


【解决方案1】:

一点点算术可以为你生成碎片:

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

【讨论】:

    【解决方案2】:
    library(lubridate)
    
    round(seconds_to_period(as.numeric(substr(df1$Time, 1, 20))*86400))
    

    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" 
    

    对于时间来说:

    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"
    

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多