It's not a huge change, but sapply will provide a vector by default.  You
can at least eliminate the unlist() call.

seq <- 1:100

l <- vector("list", 100)

for (i in seq) {l[[i]] <- list(species=seq[i], name=as.character(seq[i])) }

s <- sapply(seq, function(seq) l[[seq]]$name)



On Tue, Aug 13, 2013 at 5:39 AM, Novack-Gottshall, Philip M. <
pnovack-gottshall@ben.edu> wrote:

> All,
>
> Please excuse what may be a slightly off-topic question. I'm having
> trouble extracting elements from a list of lists.
>
> Here is an example:
>
> # Make list of lists:
>
> seq <- 1:100
>
> l <- vector("list", 100)
>
> for (i in seq) {l[[i]] <- list(species=seq[i], name=as.character(seq[i])) }
>
>
>
> # Extract second elements:
>
> l$name          # NULL
>
> l[seq]$name     # also NULL
>
> s <- unlist(lapply(seq, function(seq) l[[seq]]$name))
>
>
>
> The lapply() call works fine, but it seems quite inefficient or indirect.
> Can anyone suggest a more intuitive extraction?
>
> Thanks,
> Phil
>
>
> --
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>  Phil Novack-Gottshall
>  Assistant Professor
>  Department of Biological Sciences
>  Benedictine University
>  5700 College Road
>  Lisle, IL 60532
>
>  pnovack-gottshall@ben.edu<mailto:pnovack-gottshall@ben.edu>
>  Phone: 630-829-6514
>  Fax: 630-829-6547
>  Office: 332 Birck Hall
>  Lab: 107 Birck Hall
>  http://www1.ben.edu/faculty/pnovack-gottshall
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>



-- 
*Andrew P. Vitale*
Masters Student
Department of Geography
University of Nevada, Reno
(412) 915-3632
vitale232@gmail.com

	[[alternative HTML version deleted]]

