dplyr mutate_each_标准评估 [英] dplyr mutate_each_ standard evaluation

查看:251
本文介绍了dplyr mutate_each_标准评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在欺骗我的大脑,在dplyr中的mutate_each_的SE实现。我想做的是从DF中的每一列中减去一列DF中的一个值。



这是我正在寻找的最小工作示例要完成,使用虹膜数据集(我删除'物种'列,使其全部为数字)。我从每一列减去Petal.Width列。但是我需要列名作为变量,例如My.Petal.Width

 #删除Species列,因此我们只有数字数据
iris_numeric< - iris%>%select(-Species)

#这是所需的结果,使用NSE
result_NSE< - iris_numeric %>%mutate_each(funs(。 - 'Petal.Width`))

#这是我尝试使用SE
SubtractCol< - Petal.Width
result_SE< - iris_numeric%>%mutate_each_(funs(。 - as.name(SubtractCol)))

#第二次尝试
SubtractCol< - Petal.Width
列< - colnames(iris_numeric)
mutate_call = lazyeval :: interp(〜。-a,a = as.name(SubtractCol))
result_SE< - iris_numeric%>%mutate_each_点= setNames(列表(mutate_call),列))

我收到各种错误:

  colwise_(tbl,funs_(funs),vars)中的错误:
参数vars缺少,没有默认

mutate_each_(。,.do中的错误) ts = setNames(list(mutate_call),Columns)):
unused参数(.dots = setNames(list(mutate_call),Columns))

请帮忙,谢谢提前。

解决方案

你正在寻找什么是SE版本的 funs ,即 funs _

 库(lazyeval); library(dplyr)
SubtractCol< - Petal.Width
iris%>%mutate_each(funs_(interp(〜。-x,x = as.name(SubtractCol))), - )%>%head
#Sepal.Length Sepal.Width Petal.Length Petal.Width物种
#1 4.9 3.3 1.2 0 setosa
#2 4.7 2.8 1.2 0 setosa
#3 4.5 3.0 1.1 0 setosa
#4 4.4 2.9 1.3 0 setosa
#5 4.8 3.4 1.2 0 setosa
#6 5.0 3.5 1.3 0 setosa

如果您想将我所写的-Species作为-Species提供,您将使用 mutate_each _ 一个字符串/变量。



请注意, mutate_each _

中没有 .dot code>和 summarise_each _


I'm racking my brain over the SE implementation of mutate_each_ in dplyr. What I want to do is to subtract the value in one column in a DF from every column in the DF.

Here is a minimum working example of what I'm looking to accomplish, using the iris dataset (I remove the 'Species' column so that it is all numeric). I subtract the Petal.Width column from every column. But I need the column name to be a variable, such as "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))

I get various errors:

Error in colwise_(tbl, funs_(funs), vars) : 
  argument "vars" is missing, with no default

Error in mutate_each_(., .dots = setNames(list(mutate_call), Columns)) : 
  unused argument (.dots = setNames(list(mutate_call), Columns))

Please help and many thanks in advance.

解决方案

What you are looking for is the SE version of funs, i.e. funs_:

library(lazyeval); library(dplyr)
SubtractCol <- "Petal.Width"
iris %>% mutate_each(funs_(interp(~.-x, x = as.name(SubtractCol))), -Species) %>% head
#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#1          4.9         3.3          1.2           0  setosa
#2          4.7         2.8          1.2           0  setosa
#3          4.5         3.0          1.1           0  setosa
#4          4.4         2.9          1.3           0  setosa
#5          4.8         3.4          1.2           0  setosa
#6          5.0         3.5          1.3           0  setosa

You would use mutate_each_ if you wanted to supply what I wrote as "-Species" as a string/variable.

Note that there's no .dots argument in mutate_each_ and summarise_each_.

这篇关于dplyr mutate_each_标准评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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