Skip to content
Prev 247170 / 398503 Next

navigating in lists

Or, if for some reason the lists differ in length...

test=list(a=list(1,2),b=list(3,4),c=list(5,6,7))


picker <- function(x, i) {
if(length(x)>=i)
  x[[i]]
else
  NA
}

pick <- function(list,i) {
sapply(list, function(x) picker(x, i))
}
a b c 
1 3 5
a b c 
2 4 6
a  b  c 
NA NA  7
a  b  c 
NA NA NA 

-Erik Gregory
Student Assistant, California EPA
CSU Sacramento, Mathematics

----- Original Message ----
From: Greg Snow <Greg.Snow at imail.org>
To: Jannis <bt_jannis at yahoo.de>; "r-help at r-project.org" <r-help at r-project.org>
Sent: Wed, January 12, 2011 1:17:54 PM
Subject: Re: [R] navigating in lists
a b c 
1 3 5