Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using tidyr complete() with column names specified in variables

Tags:

r

tidyr

I am having trouble using the tidyr::complete() function with column names as variables.

The built-in example works as expected:

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)) 

However, when I try to provide the column names as character strings, it produces an error.

gr="group"
id="item_id"
name="item_name"
df %>% complete_(gr, nesting_(id, name),fill = list(NA))
like image 407
ChriiSchee Avatar asked Nov 13 '16 18:11

ChriiSchee


People also ask

How do I convert column values to column names in R?

To convert a column values to column names, we can use dcast function of reshape2 package. For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical. Now if we want to convert the categories in x as column names then it can be done as dcast(df,y~x).

How do I select multiple columns in a Dataframe in R?

To pick out single or multiple columns use the select() function. The select() function expects a dataframe as it's first input ('argument', in R language), followed by the names of the columns you want to extract with a comma between each name.


2 Answers

Even a little more simply, df %>% complete(!!!syms(gr), nesting(!!!syms(id), !!!syms(name))) now gets it done in tidyr 1.0.2

like image 95
Logit Avatar answered Nov 15 '22 12:11

Logit


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
like image 24
alistaire Avatar answered Nov 15 '22 11:11

alistaire