Komal Rathi
Komal Rathi

Reputation: 4274

Concatenate list of lists in 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?

Upvotes: 5

Views: 11427

Answers (2)

exa
exa

Reputation: 51

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"

Upvotes: 3

David Robinson
David Robinson

Reputation: 78600

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")

Upvotes: 7

Related Questions