I'm attempting to iterate over a list of column suffixes and, for each, call a function to perform a bunch of summarises. My problem is how to correctly build the variable name and have R interpret it correctly. I've tried various combinations of {{}}, !!, and sym() without success.
I can do this for the LHS in a mutate without problems, but the correct syntax for the RHS seems to elude me.
A stripped down version follows:
fsumm = function(data, lookAhead) {
summarise(data,
mean=mean("prefix{lookAhead}", na.rm=TRUE),
)
}
lookAheadList = c(2, 5, 10, 20)
for (lookAhead in lookAheadList) {
p = df %>%
fsumm( lookAhead )
print(p)
}
UPDATE: I seem to be making some progress by using:
summarise( mean=mean( !!sym(glue("prefix{lookAhead}")), na.rm=TRUE) )
I thought though that tidyval(?) was meant to allow me to drop the explicit call to glue(). Is there a simpler way of writing this?