0

I wrote some code to automate printing out a list of ANOVA models that all use the same independent variable. I created a vector of all the numeric variables in a data frame. Then I used a for loop to create the necessary ANOVA models, the name of the dependent variable in each model, & the summary of the model.

I have used this block of code before with no problems, but when I tried running it today, I got the error message "Error: object 'anova_models' not found." Is there an issue with some command being deprecated? Or is R trying to discourage creating list objects out of ANOVA models? Am I following an incorrect naming convention?

numeric_columns <- which(sapply(iris, is.numeric)) ## Creates a named integer vector of column number for numeric variables in the data frame.

for (i in 1:length(numeric_columns)) {
 anova_models[[i]] <- aov(unlist(iris[,numeric_columns[[i]])~Species, data = iris)

print(names(numeric_columns[[i]])
print(summary(anova_models[[i]])
}

Does anybody have an idea what might be keep this block of code from running?

jonp72
  • 1
  • You need to create the `anova_models` list beforehand like `anova_models <- list()` Also you are missing a square bracket `]` at the end of `unlist(iris[,numeric_columns[[i]])`. Should be `unlist(iris[,numeric_columns[[i]]])` – Humpelstielzchen Dec 16 '21 at 19:49
  • You don't need `unlist`, when you extract only one column the dimension is dropped by default. Also, Why not a `lapply` loop? And `print(names(numeric_columns[[i]]))` always prints `NULL`. – Rui Barradas Dec 16 '21 at 19:50
  • @Humpelstielzchen Creating anova_models first as a list object seems to do the trick. – jonp72 Dec 16 '21 at 20:34

0 Answers0