Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate list of lists in R

Tags:

r

I have a list TF2Gene[1326] which looks like

view(TF2Gene)

structure(list(Sp1=c("a","b","c"),p53=c("x","y","z"),Elk1=c("1","2","3"),...))

So its basically a list of 1326 lists.

Now I want to concatenate the values of these lists in 1 so that I can find the unique members. What I am doing is:

cols <- unique(unlist(TF2Gene))    

Is this correct?

like image 814
Komal Rathi Avatar asked Mar 14 '13 16:03

Komal Rathi


2 Answers

Yes, that is the correct way to do it. On the example above the result would be a vector like:

c("a", "b", "c", "x", "y", "z", "1", "2", "3")
like image 149
David Robinson Avatar answered Oct 29 '22 16:10

David Robinson


That's only going to work if your lists have atomic elements. Mine usually don't. Try this instead.

do.call(c, list (list( 3,4,5 ) , list( "a","b" )))
[[1]]
[1] 3

[[2]]
[1] 4

[[3]]
[1] 5

[[4]]
[1] "a"

[[5]]
[1] "b"
like image 45
exa Avatar answered Oct 29 '22 16:10

exa