问题
When I use the complete() function to fill in rows in my data that have no cases I find it is creating many duplicate rows as well. These can be removed with the unique() function, but I want to understand how I can avoid generating all these extra rows in the first place.
library(dplyr)
library(tidyr)
# An incomplete table
mtcars %>%
group_by(vs, cyl) %>%
count()
# complete() creates a table with many duplicate rows
temp <-
mtcars %>%
group_by(vs, cyl) %>%
count() %>%
complete(vs = c(0, 1), cyl = c(4, 6, 8), fill = list(n = 0))
unique(temp)
来源:https://stackoverflow.com/questions/48573217/why-does-complete-create-duplicate-rows-in-my-data