Home > Enterprise >  Loop-generated list of data frames not being joined by rbind properly
Loop-generated list of data frames not being joined by rbind properly

Time:08-07

I have a table with samples of data named Sample_1, Sample_2, etc. I take user input as a string for which samples are wanted (Sample_1,Sample_3,Sample_5). Then after parsing the string, I have a for-loop which I pass each sample name to and the program filters the original dataset for the name and creates a DF with calculations. I then append the DF to a list after each iteration of the loop and at the end, I rbind the list for a complete DF.

sampleloop <- function(samplenames) {
    data <- unlist(strsplit(samplenames, ","))
    temp = list()
    for(inc in 1:length(data)) {
          df <- CT[CT[["Sample_Name"]] == data[inc],]
          ........
          tempdf = goitemp
          temp[inc] <- tempdf
        }
    newdf <- do.call(rbind.data.frame, temp)
}

The inner function on its own produces the correct wanted output. However, with the loop the function produces the following wrong DF if the input is "Sample_3,Sample_9":

Incorrect DF with input "Sample_3,Sample_9"

I'm wondering if it has something to do with the rbind?

CodePudding user response:

The issue seems to be using [ instead of [[ to access and assign to the list element`

sampleloop <- function(samplenames) {
    data <- unlist(strsplit(samplenames, ","))
    temp  <- vector('list', length(data))
    for(inc in seq_along(data)) {
          df <- CT[CT[["Sample_Name"]] == data[inc],]
          ........
          tempdf <- goitemp
          temp[[inc]] <- tempdf
        }
    newdf <- do.call(rbind.data.frame, temp)
    return(newdf)
}

The difference can be noted with the reproducible example below

lst1 <- vector('list', 5)
lst2 <- vector('list', 5)
for(i in 1:5) {
  lst1[i] <- data.frame(col1 = 1:5, col2 = 6:10)
 lst2[[i]] <- data.frame(col1 = 1:5, col2 = 6:10)
}
  • Related