Search code examples
rtidyversensedplyr

dplyr NSE - how pass column names to mutate function call?


I'd like to mutate columns of a data frame using a gsub argument on columns specified in a variable, but am struggling with nonstandard evaluation.

In this toy example, I'd like to use columns[[1]] and columns[[2]] instead of .$name_A and .$name_B in my call to gsub. Can I, or do I need to rethink my approach?

library(tidyverse)

test_df <- tibble(name_A = 
                   c("asdf", "ghjk"),
                 name_B =
                   c("qwer", "tyui"))

columns <- c("name_A", "name_B")

test_df %>%
  mutate(new_col_A = 
           gsub(pattern = 'asdf', replacement = 'NEW_VALUE_A', x = .$name_A),
         new_col_B = 
           gsub(pattern = 'tyui', replacement = 'NEW_VALUE_B', x = .$name_B))

Solution

  • You're nearly there. You can use rlang::syms along with !! to do what you need.

    library(tidyverse)
    
    test_df <- tibble(name_A = 
                        c("asdf", "ghjk"),
                      name_B =
                        c("qwer", "tyui"))
    
    columns <- rlang::syms(c("name_A", "name_B"))
    
    test_df %>%
      mutate(new_col_A = 
               gsub(pattern = 'asdf', replacement = 'NEW_VALUE_A', x = !! columns[[1]]),
             new_col_B = 
               gsub(pattern = 'tyui', replacement = 'NEW_VALUE_B', x = !! columns[[2]]))