【问题标题】:make time series data frame in R ask 2 (use dplyr??)在 R 中制作时间序列数据框问 2(使用 dplyr??)
【发布时间】:2020-07-18 00:35:45
【问题描述】:

如果我有存储发票数据。 因为没有人卖出,所以错过了数据。

missed date
         day   item sale value
1 2011-01-01  apple  yes   100
2 2011-01-02  apple   no   200
4 2011-01-06 banana  yes   500

true calendar
         day  
1 2011-01-01  
2 2011-01-02  
3 2011-01-04  
4 2011-01-05  
5 2011-01-06 

我需要完整的数据,例如使用过的“tidyverse:::compleat func”。

true calendar
         day  
1 2011-01-01  
2 2011-01-02  
3 2011-01-04  
4 2011-01-05  
5 2011-01-06 

我想添加 Jan-4 和 Jan-5 日期。

bind "2011-01-01" "apple"  "yes" "100"
bind "2011-01-01" "apple"  "no"  "0"  
bind "2011-01-01" "banana" "yes" "0"  
bind "2011-01-01" "banana" "no"  "0"  
bind "2011-01-02" "apple"  "yes" "0"  
bind "2011-01-02" "apple"  "no"  "200"
bind "2011-01-02" "banana" "yes" "0"  
bind "2011-01-02" "banana" "no"  "0" 

bind "2011-01-04" "apple"  "yes" "0"
bind "2011-01-04" "apple"  "no"  "0"  
bind "2011-01-04" "banana" "yes" "0"  
bind "2011-01-04" "banana" "no"  "0"  
bind "2011-01-05" "apple"  "yes" "0"  
bind "2011-01-05" "apple"  "no"  "0"  
bind "2011-01-05" "banana" "yes" "0"
bind "2011-01-05" "banana" "no"  "0"  

bind "2011-01-06" "apple"  "yes" "0"  
bind "2011-01-06" "apple"  "no"  "0"  
bind "2011-01-06" "banana" "yes" "500"
bind "2011-01-06" "banana" "no"  "0"  

我该怎么做?在 R 语言中。

【问题讨论】:

    标签: r date time-series tidyverse


    【解决方案1】:

    我们可以使用complete 生成从最小值day 到最大值day 的所有日期,然后将right_joincalendar 一起使用以仅保留calendar 中的日期。

    library(dplyr)
    
    df %>%
      mutate(day = as.Date(day)) %>%
      tidyr::complete(item, sale, day = seq(min(day), max(day), by = 'day'), 
                      fill = list(value = 0)) %>%
      right_join(calendar %>% mutate(day = as.Date(day)), by = 'day')
    
    
    # A tibble: 20 x 4
    #   item   sale  day        value
    #   <fct>  <fct> <date>     <dbl>
    # 1 apple  no    2011-01-01     0
    # 2 apple  yes   2011-01-01   100
    # 3 banana no    2011-01-01     0
    # 4 banana yes   2011-01-01     0
    # 5 apple  no    2011-01-02   200
    # 6 apple  yes   2011-01-02     0
    # 7 banana no    2011-01-02     0
    # 8 banana yes   2011-01-02     0
    # 9 apple  no    2011-01-04     0
    #10 apple  yes   2011-01-04     0
    #11 banana no    2011-01-04     0
    #12 banana yes   2011-01-04     0
    #13 apple  no    2011-01-05     0
    #14 apple  yes   2011-01-05     0
    #15 banana no    2011-01-05     0
    #16 banana yes   2011-01-05     0
    #17 apple  no    2011-01-06     0
    #18 apple  yes   2011-01-06     0
    #19 banana no    2011-01-06     0
    #20 banana yes   2011-01-06   500
    

    数据

    df <- structure(list(day = structure(1:3, .Label = c("2011-01-01", 
    "2011-01-02", "2011-01-06"), class = "factor"), item = structure(c(1L, 
    1L, 2L), .Label = c("apple", "banana"), class = "factor"), sale = 
    structure(c(2L, 1L, 2L), .Label = c("no", "yes"), class = "factor"),
    value = c(100L, 200L, 500L)), class = "data.frame", row.names = c("1", "2", "4"))
    
    calendar <- structure(list(day = structure(1:5, .Label = c("2011-01-01", 
    "2011-01-02", "2011-01-04", "2011-01-05", "2011-01-06"), class = 
    "factor")), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))
    

    【讨论】:

    • 脚本中的“管道”很难理解......需要习惯。
    • 是的,一开始它可能会让人感到困惑,但是一旦你习惯了它就会非常方便,节省大量的打字和创建中间对象。
    • 我可以问你下一个问题吗?我将发布“在 R ask 3 中制作时间序列数据框”
    • 当然,您可以提出一个新问题。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 2018-12-07
    • 2021-03-18
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    相关资源
    最近更新 更多