【问题标题】:R - How to extract the time from a datetime column to use for graphingR - 如何从日期时间列中提取时间以用于绘图
【发布时间】:2016-03-28 02:54:51
【问题描述】:

我有一个名为 time 的列,其中包含日期和时间,以这种格式列出,例如:03/20/2016 14:24:47.153。我想提取用于生成折线图的时间部分(所以我想要一个全面的 x 轴时间间隔)。我该怎么做?

【问题讨论】:

    标签: r datetime time col


    【解决方案1】:

    我们可以使用sub从字符串中提取“时间”。

    sub("\\S+\\s+", "", str1)
    #[1] "14:24:47.153"
    

    或者转换为 Datetime 类然后format

    options(digits.secs=3)
    format(strptime(str1, format= "%m/%d/%Y %H:%M:%OS"), "%H:%M:%OS3")
    #[1] "14:24:47.153"
    

    数据

    str1 <- "03/20/2016 14:24:47.153"
    

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 2015-05-25
      • 2021-10-15
      • 2014-11-20
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 2018-10-29
      相关资源
      最近更新 更多