【问题标题】:Why does strftime change my times in R?为什么 strftime 会改变我在 R 中的时间?
【发布时间】:2017-10-30 09:14:31
【问题描述】:

我有一个包含 24 小时制时间列的数据框:

canopy_understory trap_no           file_name     date     time   temp light_intensity
              <chr>   <int>               <chr>   <dttm>   <time>  <dbl>           <dbl>
1                 c      11 trap11c_160314.txt1 16-01-09 14:00:00 35.542              41
2                 c      11 trap11c_160314.txt2 16-01-09 15:00:00 28.953               4
3                 c      11 trap11c_160314.txt3 16-01-09 16:00:00 27.468               1

为了删除秒列,我在 R 中运行此代码:

all_files_hobo$time <- strftime(all_files_hobo$time, format = "%H:%M")             

运行此代码后,我有以下数据框

canopy_understory trap_no           file_name     date  time   temp light_intensity
              <chr>   <int>               <chr>   <dttm> <chr>  <dbl>           <dbl>
1                 c      11 trap11c_160314.txt1 16-01-09 06:00 35.542              41
2                 c      11 trap11c_160314.txt2 16-01-09 07:00 28.953               4
3                 c      11 trap11c_160314.txt3 16-01-09 08:00 27.468               1

如您所见,秒数已过,列已从 变为 ,但时间也不同。这是有问题的。我可以每次都加 8,但我担心这里会发生更阴险的事情,并且在不久的将来事情会变得一团糟。为什么 strftime 改变了我的时代?

【问题讨论】:

  • 阅读这个人我认为这是一个时区问题,但我仍然不知道如何解决这个问题。
  • 使用tz参数strftime设置时区
  • 如何设置时区以使其不发生任何变化?
  • 很可能是您所在的当前时区或 UTC。见OlsonNames()

标签: r csv strftime


【解决方案1】:

由于时间列不包含 TZ 信息,因此假定为 UTC,然后strftime 在转换中使用它与本地 TZ 之间的差异。

那就试试吧:

all_files_hobo$time <- strftime(all_files_hobo$time, format = "%H:%M", tz = "UTC")

【讨论】:

    【解决方案2】:

    或者试试

    library(lubridate)
    all_files_hobo$time <-hms(all_files_hobo$time)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 2017-09-25
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多