Home > OS >  How do I specify the name of unite() in a vector?
How do I specify the name of unite() in a vector?

Time:03-18

How can I make this code work?

df <- data.frame(a = 1:5,
             b = 6:10)

name <- c('foo', 'bar')

df %>% unite(name[1], 1:2, sep = ';')

I get this error code: Error: Must supply a symbol or a string as argument

What I want basically is this:

df %>% unite(foo, 1:2, sep = ';')

Output:

   foo
1  1;6
2  2;7
3  3;8
4  4;9
5 5;10

Thank you in advance.

CodePudding user response:

I was able to do it with:

df <- data.frame(a = 1:5,
             b = 6:10)
name <- c('foo', 'bar')

df %>% unite(!!name[[1]], 1:2, sep = ';')

Using !! tells tidy verse the status of evaluation for the var in question

"In order to tell [unite()], that the variable name[1] has already been quoted we need to use the !!-Operator; pronounced Bang-Bang" !! Info

  • Related