I have an analysis that I sometimes want to perform on one set of columns and sometimes want to perform on another, similar, set of columns. The analysis is long enough that it is tedious to change all the column names within the code.
The solution I thought of was to create a variable that contained the names of the columns I was using and then just reference that variable. However, dplyr functions don't seem to be able to extract the column names from the variable.
For example
df = data.frame(color = c(rep("blue", 5), rep("red", 5)) ,
raw.values = rnorm(10), adjusted.values = rnorm(10)))
column_to_use <- "raw.values"
cool_analysis <- df %>%
group_by(color) %>%
summarise(mean = mean(column_to_use))
If this worked, I could just change column_to_use
and not have to change everywhere in the whole analysis that references raw.values
. Unfortunately, dplyr doesn't know what to do with the object. Is there a way to make this work?