【问题标题】:How can i fill in the missing rows in R [duplicate]我如何填写R中缺少的行[重复]
【发布时间】:2021-04-04 19:22:47
【问题描述】:

我的时间序列是以分钟为基础的。缺少一些分钟(请参阅第 30 到 31 行,缺少 6 分钟。如何用计数 0 填充这些分钟?所以我想添加每个缺失的分钟并用 0 填充计数。

【问题讨论】:

    标签: r time-series


    【解决方案1】:

    一种简单的方法是使用tsibble 包中的fill_gaps() 函数:

    library(tsibble)
    library(dplyr)
    
    df1 <- tibble(
        row = 1:100,
        lane = rnorm(100),
        count = sample(1:5, size=100, replace=TRUE),
        timestamp = seq(as.POSIXct("2019-11-02 00:00:00"), by= "1 min", length=100)
      ) %>%
      filter(row <= 30 | row >= 36) %>%
      select(-row)
    df1[26:35,]
    #> # A tibble: 10 x 3
    #>      lane count timestamp          
    #>     <dbl> <int> <dttm>             
    #>  1  0.218     4 2019-11-02 00:25:00
    #>  2 -1.63      4 2019-11-02 00:26:00
    #>  3  0.603     5 2019-11-02 00:27:00
    #>  4 -1.04      4 2019-11-02 00:28:00
    #>  5 -0.397     5 2019-11-02 00:29:00
    #>  6  0.179     5 2019-11-02 00:35:00
    #>  7  0.391     4 2019-11-02 00:36:00
    #>  8  1.09      5 2019-11-02 00:37:00
    #>  9  0.119     2 2019-11-02 00:38:00
    #> 10  0.949     3 2019-11-02 00:39:00
    
    df2 <- df1 %>%
      as_tsibble(index=timestamp) %>%
      fill_gaps(count=0)
    
    df2[26:35,]
    #> # A tsibble: 10 x 3 [1m] <?>
    #>      lane count timestamp          
    #>     <dbl> <dbl> <dttm>             
    #>  1  0.218     4 2019-11-02 00:25:00
    #>  2 -1.63      4 2019-11-02 00:26:00
    #>  3  0.603     5 2019-11-02 00:27:00
    #>  4 -1.04      4 2019-11-02 00:28:00
    #>  5 -0.397     5 2019-11-02 00:29:00
    #>  6 NA         0 2019-11-02 00:30:00
    #>  7 NA         0 2019-11-02 00:31:00
    #>  8 NA         0 2019-11-02 00:32:00
    #>  9 NA         0 2019-11-02 00:33:00
    #> 10 NA         0 2019-11-02 00:34:00
    

    reprex package (v0.3.0) 于 2020 年 12 月 28 日创建

    【讨论】:

      【解决方案2】:

      如果该列是Datetime 类,则从complete 中的1 minute 中的1 minute 从minmax 的'timestamp' 列的值创建一个sequence,同时将count 指定为0 表示原始数据集中将丢失的元素

      library(tidyr)
      library(dplyr)
      df2 <- complete(df1, timestamp = seq(min(timestamp), 
              max(timestamp), by = "1 min"), fill = list(count = 0))
      

      如果我们需要用相同的值填充lane 列,请使用fill

      df2 <- complete(df1, timestamp = seq(min(timestamp), 
              max(timestamp), by = "1 min"), fill = list(count = 0)) %>%
          fill(lane)
      

      注意:如果列'timestamp'不是Datetime类,可以用as.POSIXct转换成POSIXct

      df1$timestamp <- as.POSIXct(df1$timestamp) 
      

      在执行complete 步骤之前

      【讨论】:

      • 谢谢。我已经尝试过了,但它没有做任何事情,我也没有收到错误,所以我不知道出了什么问题。我的时间戳是 类,我的代码是:complete(Data_2019, timestamp=seq((min(timestamp)), max(timestamp), by = "1 min"), fill=list(count=0)) % >% 填充(车道)
      • @EvA 您是否已将其分配回同一对象或不同对象,即df2 &lt;- complete(df1, ...
      • 我现在将它分配给不同的对象。它有效:),谢谢!
      猜你喜欢
      • 2019-12-31
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 2021-11-30
      相关资源
      最近更新 更多