Skip to content
Back to formatted view

Raw Message

Message-ID: <64BBF0DA-DF27-4DCD-8372-E9BAD3D90F8A@comcast.net>
Date: 2012-02-14T14:54:29Z
From: David Winsemius
Subject: Selecting elements from all items in a list
In-Reply-To: <1329227056420-4387045.post@n4.nabble.com>

On Feb 14, 2012, at 8:44 AM, geotheory wrote:

> Basic question (if you know the answer)...  I am dealing with a list  
> of
> commonly-formatted sub-lists, for example:
>
> l <- list("")
> l[[1]] <- c("A1","A2","A3")
> l[[2]] <- c("B1","B2","B3")
> l[[3]] <- c("C1","B2","B3")
>
> Lets say I need to extract every 2nd item (i.e. A2, B2, C2).  [[]]  
> cannot be
> used.   l[2] returns practically the same as l[[2]].  Is there a way  
> to
> achieve this without having to loop or convert to data.frame?

Any such extraction would of necessity have a loop-like basis, but  
perhaps you meant without an explicit for-loop?

 > sapply(l, "[[", 2)
[1] "A2" "B2" "B2"


-- 

David Winsemius, MD
West Hartford, CT