【问题标题】:Adding values for missing rows based on a column value [duplicate]根据列值添加缺失行的值[重复]
【发布时间】:2017-11-09 16:37:08
【问题描述】:

我有一个数据框:

things <- data.frame( category = c("A","B","A","B","B","A","B"),
               things2do = c("ball","ball","bat","bat","hockey","volley ball","foos ball"),
                  number = c(12,5,4,1,2,1,1))

现在我想在缺少特定类别和 things2do 的数字中添加“0”,例如应该为“A”、“曲棍球”和“0”添加一个新行,排球和足球也是如此。

我希望我能在这里得到一些帮助。

【问题讨论】:

    标签: r row add missing-data


    【解决方案1】:

    我们可以使用来自base Rexpand.grid 来做到这一点

    d1 <- merge(expand.grid(category = unique(things$category), 
            things2do = unique(things$things2do)), things, all.x = TRUE)
    
    d1$number[is.na(d1$number)] <- 0
    d1
    #   category   things2do number
    #1         A        ball     12
    #2         A         bat      4
    #3         A   foos ball      0
    #4         A      hockey      0
    #5         A volley ball      1
    #6         B        ball      5
    #7         B         bat      1
    #8         B   foos ball      1
    #9         B      hockey      2
    #10        B volley ball      0
    

    注意:未使用任何外部包

    【讨论】:

      【解决方案2】:

      tidyrcomplete() 函数这样做:

      library(tidyr)
      
      things %>%
          complete(category, things2do, fill = list(number = 0))
      

      输出:

      # A tibble: 10 x 3
         category   things2do number
           <fctr>      <fctr>  <dbl>
       1        A        ball     12
       2        A         bat      4
       3        A   foos ball      0
       4        A      hockey      0
       5        A volley ball      1
       6        B        ball      5
       7        B         bat      1
       8        B   foos ball      1
       9        B      hockey      2
      10        B volley ball      0
      

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 1970-01-01
        • 2020-02-19
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-15
        相关资源
        最近更新 更多