Reputation: 175
I'm trying to do ANOVA with compare_means()
. Using the sample dataset ToothGrowth
as an example, I want to achieve something like this:
#directly calling column name
compare_means(len~supp, data=ToothGrowth, group.by = "dose", method = "anova")
except that my supp
in the formula is a string.
Directly passing the string as an argument results in an error in the model formula. So I tried converting the string to name or symbol:
#convert string column name to name/symbol
x <- as.name("supp") #as.symbol("supp")
compare_means(len~x, data=ToothGrowth, group.by = "dose", method = "anova")
But both returned the same error:
Error:
x
must resolve to integer column positions, not a symbol
I have checked similar error messages like Error All select() inputs must resolve to integer column positions. The following do not:, but none of them seem to address my issue.
Please instruct what I should do. Any help would be much appreciated!
Upvotes: 1
Views: 706
Reputation: 175
So I saw How to use reference variables by character string in a formula? and solved the problem using
f <- "len~supp"
do.call("compare_means", list(as.formula(f), data=as.name("ToothGrowth"),
group.by = "dose", method = "anova"))
Upvotes: 1