I'm trying to add columns to lists in a for loop and I've tried $ notation and the following reprex with no success. What's the approach for base R here?
# selected
month = c(5, 6)
# list of models
models = lapply(setNames(month, paste0("m", month)), function(x) {
lm(Temp ~ Ozone, subset(airquality, Month == x))
})
# adding residuals of every model to airquality dataset
airquality$residuals = NA
for (model in names(models)) {
temp = sub("m", "", model)
data = within(airquality, {
residuals = ifelse(
Month == temp,
models[model][, "residuals"],
residuals
)
})
}
#> Error in models[model][, "residuals"]: número incorreto de dimensões
Created on 2022-08-26 by the reprex package (v2.0.1)