How to make inner for loop only pass specific rows/elements

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))){


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!

Can't test without a completereprex including data(see the FAQ). But why not just

tab_results <- head(rbind(tab_results,temp_results),6)

Unfortunately, I can not share the data.

Using the head() did somehow work. However, I am looping though several models, and head() will cut off the outcome of the following models.

Doesn’t have to be real data. Anything with variables named the same way.

Can't test it (2) I can not share the data

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.