ChriiSchee ChriiSchee - 22 days ago 5
R Question

tidyr complete() with colnames as variables

I don't manage to make the awesome tidyr complete() function with column names as variables running. Using the built-in example:

df <- data_frame(
group = c(1:2, 1),
item_id = c(1:2, 2),
item_name = c("a", "b", "b"),
value1 = 1:3,
value2 = 4:6
)

df %>% complete(group, nesting(item_id, item_name))


...works as expected

gr="group"
id="item_id"
name="item_name"
df %>% complete_(gr, nesting_(id, name),fill = list(NA))


... however doesn't throw an error but doesn't work either

Any help appreciated!

Answer

I think it's a bug that complete_ can't work with data.frames or list columns like complete can, but here's a workaround using unite_ and separate to simulate nesting:

df %>% unite_('id_name', c(id, name)) %>% 
    complete_(c(gr, 'id_name')) %>% 
    separate(id_name, c(id, name))

## # A tibble: 4 × 5
##   group item_id item_name value1 value2
## * <dbl>   <chr>     <chr>  <int>  <int>
## 1     1       1         a      1      4
## 2     1       2         b      3      6
## 3     2       1         a     NA     NA
## 4     2       2         b      2      5