R: Error in pi[[j]] : subscript out of bounds -- rbind on a list of dataframes

Helene picture Helene · Jan 16, 2017 · Viewed 9.7k times · Source

I am trying to rbind a large list of data frames (outputDfList), which is generated by lapply a complicated function to a large table. You can recreate outputDfList by:

df1=data.frame("randomseq_chr15q22.1_translocationOrInsertion", "chr15", "63126742")
names(df1)=NULL
df2=df1=data.frame("chr18q12.1_chr18q21.33_large_insertion", "chr18 ", "63126741")
names(df2)=NULL
outputDfList=list(df1,df2)

my code is

do.call(rbind, outputDfList)

The error message I received:

Error in pi[[j]] : subscript out of bounds

I double checked the column numbers of each dataframes and they are all the same. I also tried to use "options(error=recover)" for debug, but I'm not familiar with it enough to pitch down the exact issue. Any help is appreciated. Thank you.

Answer

Konrad Rudolph picture Konrad Rudolph · Jan 16, 2017

After the update it seems that your problem is that you have invalid column names: Data frame column names must be non-null.

After correcting this, the code then works:

for (i in seq_along(outputDfList)) {
    colnames(outputDfList[[i]]) = paste0('V', seq_len(ncol(outputDfList[[i]])))
}

do.call(rbind, outputDfList)
#                                       V1     V2       V3
# 1 chr18q12.1_chr18q21.33_large_insertion chr18  63126741
# 2 chr18q12.1_chr18q21.33_large_insertion chr18  63126741

However, I’m puzzled how this situation occurred in the first place. Furthermore, the error message I’m getting with your code is still distinct from yours:

Error in match.names(clabs, names(xi)) :
names do not match previous names