【问题标题】:Inserting rows for missing data in R - with 0 quantity在 R 中插入缺失数据的行 - 数量为 0
【发布时间】:2020-06-20 22:06:53
【问题描述】:

原始数据

Person   month   Year Quantity
A         02  2018   900
A         04  2018   600
A         09  2018   300
A         04  2019   40
B         06  2018   56
B         01  2019   10
B         09  2019   20

需要的输出

Person   month   Year Quantity
A         01  2018   0
A         02  2018   900
A         03  2018   0
A         04  2018   600
A         05  2018   0
A         06  2018   0
A         07  2018   0
A         08  2018   0
A         09  2018   300
A         10  2018   0
A         11  2018   0
A         12  2018   0
A         01  2019   0
A         01  2019   0
A         02  2019   0
A         03  2019   0
A         04  2019   40
A         05  2019   0
A         06  2019   0
A         07  2019   0
A         08  2019   0
A         09  2019   0
A         10  2019   0
A         11  2019   0
A         12  2019   0
B         01  2018   0
B         02  2018   0
B         03  2018   0
B         04  2018   0
B         05  2018   0
B         06  2018   56
B         07  2018   0
B         08  2018   0
B         09  2018   0
B         10  2018   0
B         11  2018   0
B         12  2018   0
B         01  2019   10
B         02  2019   0
B         03  2019   0
B         04  2019   0
B         05  2019   0
B         06  2019   0
B         07  2019   0
B         08  2019   0
B         09  2019   20
B         10  2019   0
B         11  2019   0
B         12  2019   0

大家好,

我在 R 数据框中有上面的示例数据,我想为每个“人”缺少数据的月份添加 0 个“数量”。我错过了几个月和一年,你可以看到我想为每个人插入我的数据框。即为缺少的月份添加一个人的 0 需求的月份-年份值。 (只是为了提供一些背景,我拥有的初始数据有每个需求的日期,但我将其分组为月-年,因为我从日期字段中提取了月份和年份值,如下所示......

PersonMonthDemand <-
  DateData %>%
  mutate(month = month(DateOfDemand))%>%
  mutate(year= year(DateOfDemand))%>%
  group_by(Person, month, year) %>%
  summarise(Quantity = sum(Quantity)) 

产生如上所示的原始数据)

如果这里有专家可以提供帮助,我们将不胜感激。提前致谢。亲切的问候,加亚特里

【问题讨论】:

    标签: r


    【解决方案1】:

    创建一个左侧基表。

    为此,请获取所有客户的独特价值:

    cstmr<-data.frame(unique(orgn_data$Person))
    change column names
    names(cstmr)[1]<-'Person'
    

    使用以下方法在数据框中查找日期和年份的所有组合:

    orgn_data_year_mnth<-unique(orgn_data[c("month", "Year")])
    cross join cstmr & organ_data_year_mnth using
    cc<-merge(cstmr, organ_data_year_mnth)
    

    最后一步是加入cc上的原始数据:

    final_df<-merge(cc,orgn_data,by=c('Person','month','Year'),all.x=TRUE)
    

    然后使用 0 替换所有 NA:

    final_df[is.na(final_df)] <- 0
    

    【讨论】:

    • 啊太棒了!感谢 Jeremy 和 Hemant,这很完美。这是很棒的东西,我也学到了。非常感谢。
    • 谢谢杰里米。我是新来的,所以编辑很有帮助!
    【解决方案2】:

    我们可以从tidyr使用complete

    library(tidyr)
    library(dplyr)
    df1 %>%
        complete(Person, month = 1:12, Year, fill = list(Quantity = 0)) %>% 
        arrange(Person, Year)
    # A tibble: 48 x 4
    #  Person month  Year Quantity
    #   <chr>  <int> <int>    <dbl>
    # 1 A          1  2018        0
    # 2 A          2  2018      900
    # 3 A          3  2018        0
    # 4 A          4  2018      600
    # 5 A          5  2018        0
    # 6 A          6  2018        0
    # 7 A          7  2018        0
    # 8 A          8  2018        0
    # 9 A          9  2018      300
    #10 A         10  2018        0
    # … with 38 more rows
    

    ###数据

    df1 <- structure(list(Person = c("A", "A", "A", "A", "B", "B", "B"), 
        month = c(2L, 4L, 9L, 4L, 6L, 1L, 9L), Year = c(2018L, 2018L, 
        2018L, 2019L, 2018L, 2019L, 2019L), Quantity = c(900L, 600L, 
        300L, 40L, 56L, 10L, 20L)), class = "data.frame", row.names = c(NA, 
    -7L))
    

    【讨论】:

    • 非常感谢 Dr.Arun,非常感谢您如此快速的回复。该代码有效,但不幸的是部分有效。抱歉,可能是我没有完全解释我的问题。对于“C2”人,非零值在 2018 年存在 1 或 2 个月,但在 2019 年和 2020 年没有。但是,对于这个人“C”,我希望填充 2019 年和 2020 年(截至当前月份)的所有月份数量也为 0。目前,您帮助处理的代码正在填充全年所有月份,但仅适用于 2018 年,因为那是 Pesron C 出现一些需求数量的唯一年份。
    猜你喜欢
    • 2015-10-01
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多