【问题标题】:How to add 1 minute to each value in a vector in R Studio?如何在 R Studio 中为向量中的每个值添加 1 分钟?
【发布时间】:2020-11-24 00:22:46
【问题描述】:

我目前正在使用向量从我的数据集中根据关闭时间(格式为 POSIXct)提取某些行:

Vector.Time   <- c('2020-03-06 10:09:11', 
                   '2020-03-06 10:13:11',
                   '2020-03-06 10:18:12')

我使用的其中一个工具在每分钟结束时记录数据,因此我需要引用第二个向量,将 1 分钟添加到原始向量中的所有值。有没有不需要创建新向量的简单方法?

【问题讨论】:

    标签: r vector time posixct


    【解决方案1】:

    您可以使用基础 R 为 POSIXct 对象添加/减去时间,它是按秒完成的。所以要在Vector.Time 中添加 1 分钟,您可以添加 60 秒。

    as.POSIXct(Vector.Time) + 60
    

    【讨论】:

      【解决方案2】:

      使用lubridate中的minutes

      library(lubridate)
      as.POSIXct(Vector.Time) + minutes(1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-15
        • 2018-12-13
        • 1970-01-01
        • 2014-02-20
        • 1970-01-01
        相关资源
        最近更新 更多