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_.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With