Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr's mutate_each within function works but matches() does not find argument

Tags:

r

dplyr

When trying to solve this problem I encountered a problem with mutate_each of dplyr. I wanted to use it within a function and pass arguments to it. It was successful for funs() but not for matches().

Let me show a simple example where the task is to append some tag the values of some variables.

library(dplyr) 
mydf <- data.frame(this_var1 = c("a", "b", "c", "d", "e"), 
                   this_var2 = c("b", "c", "d", "e", "f"),
                   that_var1 = c("x", "y", "z", "w", "u"))

mymutate1 <- function(data, tag) {
  data %>% mutate_each(funs(paste0(., tag)))
}

mymutate1(mydf, "_foo")
  this_var1 this_var2 that_var1
1     a_foo     b_foo     x_foo
2     b_foo     c_foo     y_foo
3     c_foo     d_foo     z_foo
4     d_foo     e_foo     w_foo
5     e_foo     f_foo     u_foo    

This works like a charm. However, if I try to control also for which variables the transformation should be applied it fails.

mymutate2 <- function(data, tag, m) {
  data %>% mutate_each(funs(paste0(., tag)), matches(m))
}
mymutate2(mydf, "_foo", "this")

This gives the following error: Error in is.string(match) : object 'm' not found. Why is tag found whereas m not?

The code itself works as intended:

mydf %>% mutate_each(funs(paste0(., "_foo")), matches("this"))
  this_var1 this_var2 that_var1
1     a_foo     b_foo         x
2     b_foo     c_foo         y
3     c_foo     d_foo         z
4     d_foo     e_foo         w
5     e_foo     f_foo         u
like image 967
janosdivenyi Avatar asked Mar 11 '15 13:03

janosdivenyi


1 Answers

You are going to want to use the Standard Evaluation (SE) version of mutate_each -- that is, mutate_each_:

mymutate2 <- function(data, tag, m) {
  data %>% mutate_each_(funs(paste0(., tag)), ~matches(m))
}

For additional clarity, the following is equivalent:

mymutate3 <- function(data, tag, m) {
  data %>% mutate_each_(~paste0(., tag), ~matches(m))
}

And per the vignette:

"It’s best to use a formula [ ~ as opposed to quote() or using strings "" ], because a formula captures both the expression to evaluate, and the environment in which it should be a evaluated...Using anything other than a formula will fail because it doesn't know which environment to look in."

like image 118
JasonAizkalns Avatar answered Oct 18 '22 06:10

JasonAizkalns