【问题标题】:Complete missing time series data with subgroups用子组完成缺失的时间序列数据
【发布时间】:2018-06-18 20:47:20
【问题描述】:

我有以下数据集:

在 Python 中创建 df:

df = pd.DataFrame([['04-01','04-02','04-02','04-03','04-05']['Red','Blue', 'Yellow','Red', 'Blue'], [1,2,1,1,2], [10,20,10,10,20]]).T
df.columns = ['date','color','quant','revenue'] 

或者在 R 中:

date = c('04-01','04-02','04-02','04-03','04-05')
color = c('Red', 'Blue', 'Yellow','Red', 'Blue')
quant = c(1,2,1,1,2)
revenue = c(10,20,10,10,20)
df = data.frame(date, color, quant, revenue)

df:

date    color quant revenue
04-01     Red     1      10
04-02    Blue     2      20
04-02  Yellow     1      10
04-03     Red     1      10
04-05    Blue     2      20

我想为每种颜色(Red, Blue, Yellow)填写所有缺少的日期(从04-01 到04-05),在quant 和revenue 列中填写0,输出如下:

date    color quant revenue
04-01     Red     1      10
04-01    Blue     0       0
04-01  Yellow     0       0
04-02     Red     0       0
04-02    Blue     2      20
04-02  Yellow     1      10
04-03     Red     1      10
04-03    Blue     0       0
04-03  Yellow     0       0
04-04     Red     0       0
04-04    Blue     0       0
04-04  Yellow     0       0
04-05     Red     0       0
04-05    Blue     2      20
04-05  Yellow     0       0

【问题讨论】:

    标签: python r missing-data


    【解决方案1】:

    这是在R 中使用tidyverse 的选项,我们将separate '日期' 转换为数字'day' 'year' 列,然后使用complete 扩展数据集和unite 'day ', 'year' 创建 'date' 列

    library(tidyverse)
    df %>% 
        separate(date, into = c('day', 'year'), convert = TRUE) %>% 
        complete(day, year = min(year):max(year), color,
                   fill = list(quant = 0, revenue = 0)) %>%
        mutate_at(vars(day, year), funs(sprintf('%02d', .))) %>%
        unite(date, day, year, sep='-')
    # A tibble: 15 x 4
    #   date  color  quant revenue
    #   <chr> <fct>  <dbl>   <dbl>
    # 1 04-01 Blue       0       0
    # 2 04-01 Red        1      10
    # 3 04-01 Yellow     0       0
    # 4 04-02 Blue       2      20
    # 5 04-02 Red        0       0
    # 6 04-02 Yellow     1      10
    # 7 04-03 Blue       0       0
    # 8 04-03 Red        1      10
    # 9 04-03 Yellow     0       0
    #10 04-04 Blue       0       0
    #11 04-04 Red        0       0
    #12 04-04 Yellow     0       0
    #13 04-05 Blue       2      20
    #14 04-05 Red        0       0
    #15 04-05 Yellow     0       0
    

    【讨论】:

      【解决方案2】:

      一个没有明确按日期排序的基于 R 的解决方案:

      df2 <- expand.grid(date = paste0("04-0", 1:5), 
                         color = c('Red', 'Blue', 'Yellow')) 
      df2 <- merge(df2, df, by = c("date", "color"), all.x = TRUE) 
      df2$quant[is.na(df2$quant)] <- 0 
      df2$revenue[is.na(df2$revenue)] <- 0
      
      df2
      #    date  color quant revenue 
      # 1  04-01   Blue     0       0 
      # 2  04-01    Red     1      10 
      # 3  04-01 Yellow     0       0 
      # 4  04-02   Blue     2      20 
      # 5  04-02    Red     0       0 
      # 6  04-02 Yellow     1      10 
      # 7  04-03   Blue     0       0 
      # 8  04-03    Red     1      10 
      # 9  04-03 Yellow     0       0 
      # 10 04-04   Blue     0       0 
      # 11 04-04    Red     0       0 
      # 12 04-04 Yellow     0       0 
      # 13 04-05   Blue     2      20 
      # 14 04-05    Red     0       0 
      # 15 04-05 Yellow     0       0
      

      【讨论】:

        【解决方案3】:

        这可以通过tidyr 包中的complete() 函数轻松完成。例如

        library(tidyr)
        df %>% 
          mutate(date=factor(date, levels=sprintf("04-%02d", 1:5))) %>% 
          complete(date, color, fill=list(quant=0, revenue=0))
        
        #    date  color  quant revenue
        #    <fct> <fct>  <dbl>   <dbl>
        #  1 04-01 Blue       0       0
        #  2 04-01 Red        1      10
        #  3 04-01 Yellow     0       0
        #  4 04-02 Blue       2      20
        #  5 04-02 Red        0       0
        #  6 04-02 Yellow     1      10
        #  7 04-03 Blue       0       0
        #  8 04-03 Red        1      10
        #  9 04-03 Yellow     0       0
        # 10 04-04 Blue       0       0
        # 11 04-04 Red        0       0
        # 12 04-04 Yellow     0       0
        # 13 04-05 Blue       2      20
        # 14 04-05 Red        0       0
        # 15 04-05 Yellow     0       0
        

        【讨论】:

        • 你怎么能把df$date == "04-04"的情况也归咎于?
        • @ChrisHolbrook 您可以通过为所涉及的因素设置适当的水平来让 R 了解“缺失”值。我已经更新了答案以执行mutate()(来自dplyr),让R 准确地将date 扩展到哪些值。
        猜你喜欢
        • 2018-07-21
        • 2019-09-22
        • 2021-06-21
        • 2022-01-06
        • 2016-11-21
        • 1970-01-01
        • 2018-02-22
        • 2016-11-04
        • 1970-01-01
        相关资源
        最近更新 更多