根据列值添加缺失行的值

Adding values for missing rows based on a column value

我有一个数据框:

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"、"hockey" 和“0”添加新行,排球和足球也是如此。

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

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

我们可以用 base R

中的 expand.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

注意:未使用任何外部包