rlistdatatable

list to dataframe without unique columns


I have this loop to generate some values

for (j in 1:2)  {
  table <- rep(data.frame( 
    matrix(c(letters[1:2], 
             sample(c(rep(1,100),0), size = 1),
             sample(c(rep(0,100),1), size = 1)),  ncol = 2) ), j)
}

I would like to get this output like this

X1 X2
a  1
b  0
a  1
b  1

To get table of letters with one column and numbers in second column

I tried

do.call(rbind, table)
data.frame(matrix(unlist(table), nrow=length(table), byrow=TRUE))

But I am not able to get values to right column in data table.


Solution

  • The table is getting updated in each of the iteration. Instead, we may use replicate to create a list

    lst1 <- replicate(2,  data.frame( 
        matrix(c(letters[1:2], 
                 sample(c(rep(1,100),0), size = 1),
                 sample(c(rep(0,100),1), size = 1)),  ncol = 2) ), simplify = FALSE)
    do.call(rbind, lst1)