Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a list of lists to a table, with the names of each list as a column?

Tags:

list

r

I have a fairly basic question about how to write a list to a file. I have a list generated by Mfuzz acore function, that lists the names of all the probes I have in each of 20 clusters in the following format:

[[1]]
         NAME  MEM.SHIP
ILMN_X ILMN_X 0.9993195

.
.
.
[[20]]
         NAME  MEM.SHIP
ILMN_Y ILMN_Y 0.9982345

I want to convert it to a data frame and eventually to an output file where the list number is included as a column;

Like this:

CLUSTER NAME    MEM.SHIP
      1 ILMN_X 0.9993196
      .
      .
      .
     20 ILMN_Y 0.9982345

Where the CLUSTER column indicates which sub-list the probe belongs to. Each probe name can belong to multible sub-lists. I have tried different things like suggestions in other posts to use plyr but I always just end up with a single list of all the variables without an indication of which sub-list they belonged to.

Thanks!

like image 203
mdottir Avatar asked Dec 28 '22 14:12

mdottir


1 Answers

If your original list is called clstrs, I believe this is one solution:

do.call(rbind, lapply(seq_along(clstrs), function(i){
  data.frame(CLUSTER=i, clstrs[[i]])
}))
like image 81
Nick Sabbe Avatar answered Dec 31 '22 03:12

Nick Sabbe