dplyr的mutate_each在函数中工作,但matches()没有找到参数 [英] dplyr's mutate_each within function works but matches() does not find argument

查看:119
本文介绍了dplyr的mutate_each在函数中工作,但matches()没有找到参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试解决这个问题我遇到了 mutate_each dplyr 的问题。我想在函数中使用它并传递参数。对于 funs()而不是 matches()

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

这会产生以下错误: is.string(match)中的错误:对象'm'未找到。为什么找到标签 m 不是?

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

代码本身正常工作:

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


推荐答案

您将要使用标准评估(SE)版本的 mutate_each - 也就是说, mutate_each _

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

根据小插曲

最好使用公式[而不是 quote )或使用字符串],因为一个公式捕获要评估的表达式以及它应该被评估的环境。使用公式以外的其他任何内容都会失败,因为它不知道要查找哪个环境。

"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."

这篇关于dplyr的mutate_each在函数中工作,但matches()没有找到参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆