I would like to manipulate a named vector within a list, and do that for a large number of similarly named lists. Specifically, my lists are results from glm
, and I want to change the names of the coefficients
list element.
Here's a toy example:
model_1 <- glm(Petal.Width ~ Sepal.Length*Sepal.Width, data = iris)
model_2 <- glm(Petal.Length ~ Sepal.Length*Sepal.Width, data = iris)
The desired manipulation for one list:
names(model_1$coefficients) <- c("Constant", "Length", "Width", "Length * Width")
Now trying to do this for both lists:
for (i in 1:2) {
list_name <- paste("model", i, sep = ""),
names(list_name$coefficients) <- c("Constant", "Length", "Width", "Length * Width")
}
But of course, this does not work because R tries to evaluate a list called "list_name". How could I make it evaluate the list named as the variable "list_name"?