dplyr 的 mutate_each 在函数中有效但 matches() 找不到参数
dplyr's mutate_each within function works but matches() does not find argument
在尝试解决 时,我遇到了 dplyr
的 mutate_each
问题。我想在函数中使用它并将参数传递给它。 funs()
成功,但 matches()
.
失败
让我展示一个简单的例子,其中的任务是将一些标签附加到一些变量的值。
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
这很有魅力。但是,如果我还尝试控制应为哪些变量应用转换,则会失败。
mymutate2 <- function(data, tag, m) {
data %>% mutate_each(funs(paste0(., tag)), matches(m))
}
mymutate2(mydf, "_foo", "this")
这会产生以下错误:Error in is.string(match) : object 'm' not found
。为什么 tag
被发现而 m
没有?
代码本身按预期工作:
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
您将要使用 mutate_each
的 Standard Evaluation (SE) 版本——即 mutate_each_
:
mymutate2 <- function(data, tag, m) {
data %>% mutate_each_(funs(paste0(., tag)), ~matches(m))
}
为了更加清楚,以下是等效的:
mymutate3 <- function(data, tag, m) {
data %>% mutate_each_(~paste0(., tag), ~matches(m))
}
根据 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."
在尝试解决 dplyr
的 mutate_each
问题。我想在函数中使用它并将参数传递给它。 funs()
成功,但 matches()
.
让我展示一个简单的例子,其中的任务是将一些标签附加到一些变量的值。
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
这很有魅力。但是,如果我还尝试控制应为哪些变量应用转换,则会失败。
mymutate2 <- function(data, tag, m) {
data %>% mutate_each(funs(paste0(., tag)), matches(m))
}
mymutate2(mydf, "_foo", "this")
这会产生以下错误:Error in is.string(match) : object 'm' not found
。为什么 tag
被发现而 m
没有?
代码本身按预期工作:
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
您将要使用 mutate_each
的 Standard Evaluation (SE) 版本——即 mutate_each_
:
mymutate2 <- function(data, tag, m) {
data %>% mutate_each_(funs(paste0(., tag)), ~matches(m))
}
为了更加清楚,以下是等效的:
mymutate3 <- function(data, tag, m) {
data %>% mutate_each_(~paste0(., tag), ~matches(m))
}
根据 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."