I've written a function which takes symbol as input and then is converted to a quosure by enquo() then evaluated in tidyverse functions by the !! operator. A simplified version of the function is as follows (the full function returns a dataframe and does more instead of just calculating a number but just for illustration purposes it should suffice):
query <- function(test){
#colnames are different tests, obtain quosure of the test name
test = enquo(test)
avg_test_score = scores %>%
group_by(student) %>% summarise(mean.score = mean(!!test))
}
The function works fine if I do query(english.test) for example. However, now I want to put it in a for loop for all the different columns and get their names and run it through the function then store the output into a list
output_list = list()
for (i in length(test.name)){
this.test = test.name[i] #test.name is a vector of strings
output_list[[i]] = query(test = this.test)
}
But I get this error:
In mean.default(~this.test) :
argument is not numeric or logical: returning NA
So I've kind of figured out I have to convert the character string into a symbol before inputting it into the function but I've tried different ways of converting, such as test.name[i] %>% as.symbol() or test.name[i] %>% rlang::sym() yet they still don't quite work. Is there any way to properly handle these conversions so the for loop can run my function? Thanks!