Hi R community,
I am trying to make a loop that will go through several regressions of different predictors and outcomes, and then store it in a table. For now I am trying to build the structure, and I have problems specifying the rows I wish to store in a table.
This is my code so far:
for (i in 1:length(sleep_list)){
# first loop is going though the covariance list = the list of predictors used
for(b in colnames(sleep_items[ ,grepl("numeric", colnames(sleep_items))])){
#sapply(b, as.factor)
reg_model <- polr(formula = as.formula(paste0(b,"~",sleep_list[[i]],"+",covariates)),
data = sleep_items, Hess = T)
for (n in length(row.names(summary(reg_model)$coefficients))){
Predictor<-paste(sleep_list[i])
Rating_scale<-paste(b)
Sleep<-row.names(summary(reg_model)$coefficients)[n]
temp_results<-data.frame(Predictor=Predictor,
Rating_scale=Rating_scale,
Sleep=Sleep)
tab_results<-rbind(tab_results,temp_results)
}
}
}
My output is okay, but I wish to have less outputs in the 3ed inner loop. This is what i get:
I only wish to get the rows from somn_A until alcohol_cat.
I have tried to vary the loop description:
for (n in length(row.names(summary(reg_model)$coefficients)))
However, I can not solve it.
Do you have any tips?
Thank you!