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.