Append data frames together in a for loop

后端 未结 8 1666
無奈伤痛
無奈伤痛 2020-11-28 19:21

I have a for loop which produces a data frame after each iteration. I want to append all data frames together but finding it difficult. Following is what I am

相关标签:
8条回答
  • 2020-11-28 20:09

    Again maRtin is correct but for this to work you have start with a dataframe that already has at least one column

    model <- #some processing
    df <- data.frame(col1=model)
    
    for (i in 2:17)
    {
         model <- # some processing
         nextcol <-  data.frame(model)
         colnames(nextcol) <- c(paste("col", i, sep="")) # rename the comlum
         df <- cbind(df, nextcol)
    }
    
    0 讨论(0)
  • 2020-11-28 20:15

    For me, it worked very simply. At first, I made an empty data.frame, then in each iteration I added one column to it. Here is my code:

    df <- data.frame(modelForOneIteration)
    for(i in 1:10){
      model <- # some processing
      df[,i] = model
    }
    
    0 讨论(0)
提交回复
热议问题