bheavner
bheavner

Reputation: 549

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

Upvotes: 0

Views: 692

Answers (2)

Zafar
Zafar

Reputation: 2016

I'm not 100% sure what you're asking, but you can use regex to apply mutate to columns using mutate_at and matches:

library(tidyverse)

test_df %>% 
  mutate_at(vars(matches('A')), 
            function(.x) gsub(pattern = 'asdf', 
                              replacement = 'NEW_VALUE_A', .x)) %>% 
  mutate_at(vars(matches('B')), 
            function(.x) gsub(pattern = 'tyui', 
                         replacement = 'NEW_VALUE_B', .x))

Hope that helps!

EDIT - you can also call by index:

test_df %>% 
mutate_at(vars(1), 
          function(.x) gsub(pattern = 'asdf',
                            replacement = 'NEW_VALUE_A', .x)) %>%
mutate_at(vars(2),
          function(.x) gsub(pattern = 'tyui',
                            replacement = 'NEW_VALUE_B', .x))

Upvotes: 0

Davis Vaughan
Davis Vaughan

Reputation: 2950

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

Upvotes: 2

Related Questions