Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract elements from list of lists

Tags:

I am a newbie to R. I have a list t1 in R which looks like

[[1]] [[1]][[1]] [1] "a"       "control"   [[2]] [[2]][[1]] [1] "a"        "disease1"   [[3]] [[3]][[1]] [1] "a"        "disease2"   [[4]] [[4]][[1]] [1] "b"       "control"   [[5]] [[5]][[1]] [1] "b"        "disease1"   [[6]] [[6]][[1]] [1] "b"        "disease2" 

I need to get a unique list of first elements into a vector i.e ["a", "b"] from this vector t1. How can I do this?

like image 306
rlpatrao Avatar asked Jan 17 '13 10:01

rlpatrao


1 Answers

rapply offers yet another option:

unique(rapply(t1, function(x) head(x, 1))) 
like image 191
Matthew Plourde Avatar answered Oct 14 '22 03:10

Matthew Plourde