Skip to content
Back to formatted view

Raw Message

Message-ID: <971536df050516175661b13aca@mail.gmail.com>
Date: 2005-05-17T00:56:31Z
From: Gabor Grothendieck
Subject: how to use list index to get vector
In-Reply-To: <27583b4005051617502c0545b0@mail.gmail.com>

On 5/16/05, Luke <jyzz88 at gmail.com> wrote:
> I have a simple question, but I couldn't find the answer in R manuals.
> 
> Assume I have a list:
> > foo <- list()
> > foo[[1]] <- c(1, 2, 3)
> > foo[[2]] <- c(11,22,33)
> > foo[[3]] <- c(111,222,333)
> > foo
> [[1]]
> [1] 1 2 3
> 
> [[2]]
> [1] 11 22 33
> 
> [[3]]
> [1] 111 222 333
> 
> How to use list index to get a vector of, say, the first elements of
> list elements?
> That is, how to get a vector c(1, 11, 111) from foo?
> 
> foo[[]][1] doesn't work.
> 

Try this:

sapply(foo, "[", 1)