How can I use mutate (my presumption is that I am looking for a standard estimate in my case and therefore mutate_ , but I'm not quite sure about this) when using a function that takes a list of variable names, for example:
createSum = function(data, variableNames) { data %>% mutate_(sumvar = interp(~ sum(var, na.rm = TRUE), var = as.name(paste(as.character(variableNames), collapse =",")))) }
Here is the MWE, which divides the function into its core logic and demonstrates what I'm trying to achieve:
library(dplyr) library(lazyeval) # function to make random table with given column names makeTable = function(colNames, sampleSize) { liSample = lapply(colNames, function(week) { sample = rnorm(sampleSize) }) names(liSample) = as.character(colNames) return(tbl_df(data.frame(liSample, check.names = FALSE))) } # create some sample data with the column name patterns required weekDates = seq.Date(from = as.Date("2014-01-01"), to = as.Date("2014-08-01"), by = "week") dfTest = makeTable(weekDates, 10) # test mutate on this table dfTest %>% mutate_(sumvar = interp(~ sum(var, na.rm = TRUE), var = as.name(paste(as.character(weekDates), collapse =","))))
The expected result here is what will be returned:
rowSums(dfTest[, as.character(weekDates)])
source share