I am looking to run multiple ANOVAs in R, so I was hoping to write a function.
df = iris
run_anova <- function(var1,var2,df) {
fit = aov(var1 ~ var1 , df)
return(fit)
}
In the iris dataset, the column names are "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "Species"
Assuming that I want to use these columns in the equations, how do I pass them into the run_anova function? I have tried passing them in as strings
run_anova("Sepal.Width", "Petal.Length", df)
that doesn't work because this error appears: "In storage.mode(v) <- "double" :"
run_anova(Sepal.Width, Petal.Length, df)
When I just pass them in without the quotes, "not found". How can I pass these names of the df columns into the function?
Many thanks in advance for your help.
fit = aov(as.formula(paste(var1, "~", var2)) , df)
– Benreformulate(var2, var1)
for short – rawr