相关疑难解决方法(0)

dplyr mutate_each_标准评估

我在dplyr的mutate_each_的SE实现中绞尽脑汁.我想要做的是从DF中的每一列中减去DF中一列中的值.

这是我想要完成的最小工作示例,使用虹膜数据集(我删除'Species'列,使其全部为数字).我从每列中减去Petal.Width列.但是我需要列名是变量,例如"My.Petal.Width"

# Remove Species column, so that we have only numeric data
iris_numeric <- iris %>% select(-Species)

# This is the desired result, using NSE
result_NSE <- iris_numeric %>% mutate_each(funs(. - `Petal.Width`))

# This is my attempt at using SE 
SubtractCol <- "Petal.Width"
result_SE <- iris_numeric %>% mutate_each_(funs(. - as.name(SubtractCol)))

# Second attempt
SubtractCol <- "Petal.Width"
Columns <- colnames(iris_numeric)
mutate_call = lazyeval::interp(~.-a, a = as.name(SubtractCol))
result_SE <- iris_numeric %>% mutate_each_(.dots = setNames(list(mutate_call), Columns))
Run Code Online (Sandbox Code Playgroud)

我收到各种错误:

Error in colwise_(tbl, funs_(funs), vars) …
Run Code Online (Sandbox Code Playgroud)

r dplyr

4
推荐指数
1
解决办法
1344
查看次数

标签 统计

dplyr ×1

r ×1