Why does complete() create duplicate rows in my data?

陌路散爱 提交于 2019-12-24 08:38:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!