I have a data sheet with 40 data columns (40 different nutrients), with additional columns for plot numbers and factors. I would like to automatically loop through each column name and produce a linear model and summary for each. The data columns begin at column 10.
for(i in 10:ncol(df)) { # for-loop over columns
mod2<-aov(i~block+tillage*residue+Error(subblock),data=df)
summary(mod2)
}
This is currently producing the error Error in model.frame.default(formula = i ~ subblock, data = df, drop.unused.levels = TRUE) : variable lengths differ (found for 'subblock')
Variable lengths are consistent so I imagine I am looping incorrectly.
The data looks similar to below (with more categorical columns at the start), with the nutrient columns beginning at column 10.
block | tillage | residue | subblock | nutrient 1 | nutrient 2 | etc. |
---|---|---|---|---|---|---|
b1 | NT | NR | s1 | 0.5 | 0.6 |