【问题标题】:Why do i get NA using the interval function in lubridate?为什么我使用 lubridate 中的区间函数得到 NA?
【发布时间】:2021-02-22 08:08:09
【问题描述】:

我有一个数据框,其日期排序如下所示。我尝试使用 lubridate 中的间隔函数从 POSIXct DateTime 列中获取具有间隔的列。那就是:

Interval_DateTime = interval(DateTime, lead(DateTime))

我不明白为什么我每隔三行就会得到NA,有人知道为什么吗?

DateTime

2020-09-17 06:00:00      

2020-09-17 14:00:00      

2020-09-17 22:00:00       

2020-09-18 06:00:00       

2020-09-18 14:00:00      

2020-09-18 22:00:00       

...


Interval_DateTime

2020-09-17 06:00:00 UTC--2020-09-17 14:00:00 UTC

2020-09-17 14:00:00 UTC--2020-09-17 22:00:00 UTC

NA

2020-09-18 06:00:00 UTC--2020-09-18 14:00:00 UTC

2020-09-18 14:00:00 UTC--2020-09-18 22:00:00 UTC

NA

...

【问题讨论】:

    标签: r intervals lubridate


    【解决方案1】:

    其中一个原因可能是您的数据已分组。先尝试取消分组数据。

    library(dplyr)
    library(lubridate)
    
    df %>%
      ungroup %>%
      mutate(Interval_DateTime = interval(DateTime, lead(DateTime))) -> df
    
    df
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2011-12-18
      相关资源
      最近更新 更多