Search code examples
rfunctionparameterslocal-variablesunnest

How to use a variable value as column name in unnest_tokens


I have the following code:

df <- tibble(c1 = 1:3, c2 = c("This is text1",
                              "This is text2",
                              "This is text3"))

#This works ok!
unnest_tokens(df, input=c2,
                  output=word)

#This works ok!
unnest_tokens(df, input="c2",
                 output=word)

#Why this one doesn't work?
a = "c2"
unnest_tokens(df, input=a,
                  output=word)

As can be seen above, unnest_tokens accepts both c2 per se (the column name as a variable) and "c2" (the column name as a string).

But I'd like to be able to use the third option. Pass "c2" as the value of a variable, let's say a and than use a value as the column name.

Is this possible to be done in R's tidytext package function unnest_tokens?


Solution

  • It has to do with quoting within the tidyverse. Try this with !!.

    a = "c2"
    unnest_tokens(df, input=!!a,
                      output=word)
    
    # A tibble: 9 x 2
         c1 word 
      <int> <chr>
    1     1 this 
    2     1 is   
    3     1 text1
    4     2 this 
    5     2 is   
    6     2 text2
    7     3 this 
    8     3 is   
    9     3 text3
    

    A great resource for all of this is Hadley Wickham's "Advanced R".

    19.4.1 Unquoting one argument

    Use !! to unquote a single argument in a function call.