[R] Extracting multiple elements from a list

Waichler, Scott R Scott.Waichler at pnl.gov
Thu Jan 15 21:54:35 CET 2004


Brian described well the operation I would like to do.
I'm not familiar with do.call() but I'll work on that.
Yes, ideally I would like to access values throughout a list object
with fully implict indexing, such as the invalid "alist[[1:2]]$vec[c(2, 4)]".
Notice I was hoping to subset anywhere in the data structure.
Since I can't do this subsetting with indexing directly, I was looking for
handy (and hopefully fast) functions that could be defined
generically and then called with arguments.  The use of sapply()
and lapply() with function(i) seem promising, but do not quite
cover the functionality I was looking for.

The basic application of sapply() suggested by Andy 
is fine but I can't access part of the second-level list, only the whole
vector.  Roger's use of sapply() 

 as.vector(sapply(alist, "[[", "vec"))

is a nice way to get the whole vector also, and I appreciate learning 
that syntax.  The correction by Roger for his use of lapply() still isn't 
right though (see below).

alist <- lapply(seq(along = alist), function(i) {
                 alist[[i]]$name <- new.names[i]
                 alist
          })

I desire:
> alist
[[1]]
[[1]]$name
[1] "one"

[[1]]$vec
[1] 1 2 3 4


[[2]]
[[2]]$name
[1] "two"

[[2]]$vec
[1] 5 6 7 8

Roger's use of lapply() give: 
> alist
[[1]]
[[1]][[1]]
[[1]][[1]]$name
[1] "one"

[[1]][[1]]$vec
[1] 1 2 3 4


[[1]][[2]]
[[1]][[2]]$name
[1] "two"

[[1]][[2]]$vec
[1] 5 6 7 8



[[2]]
[[2]][[1]]
[[2]][[1]]$name
[1] "one"

[[2]][[1]]$vec
[1] 1 2 3 4


[[2]][[2]]
[[2]][[2]]$name
[1] "two"

[[2]][[2]]$vec
[1] 5 6 7 8

> -----Original Message-----
> From: Roger D. Peng [mailto:rpeng at jhsph.edu]
> Sent: Thursday, January 15, 2004 12:24 PM
> To: Waichler, Scott R
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] Extracting multiple elements from a list
> 
> 
> Sorry, that second example should be
> 
> alist <- lapply(seq(along = alist), function(i) {
>                  alist[[i]]$name <- new.names[i])
>                  alist
>           })
> 
> -roger
> 
> Roger D. Peng wrote:
> > For the first case, I actually do this pretty often and usually use 
> > something like:
> > 
> > as.vector(sapply(alist, "[[", "vec"))
> > 
> > For the second case, I think you just need to use lapply():
> > 
> > alist <- lapply(seq(along = alist), function(i)
> >         alist[[i]]$name <- new.names[i])
> > 
> > -roger
> > 
> > Waichler, Scott R wrote:
> > 
> >> For a long time I've wanted a way to conveniently extract multiple 
> >> elements
> >> from a list, which [[ doesn't allow.  Can anyone suggest 
> an efficient
> >> function to do this?  Wouldn't it be a sensible addition to R?
> >>
> >> For example,
> >>
> >> alist <- list()
> >> alist[[1]] <- list()
> >> alist[[1]]$name <- "first"
> >> alist[[1]]$vec <- 1:4
> >> alist[[2]] <- list()
> >> alist[[2]]$name <- "second"
> >> alist[[2]]$vec <- 5:8
> >> both.vec <- c(alist[[1]]$vec, alist[[2]]$vec)
> >>
> >> Can I get both.vec without c() or an explicit loop?
> >>
> >> and
> >>
> >> new.names <- c("one", "two")
> >> alist[[1]]$name <- new.names[1]
> >> alist[[2]]$name <- new.names[2]
> >>
> >> Could I assign the new values in a quasi-vectorized way?
> >>
> >> Thanks,
> >> Scott Waichler
> >> Pacific Northwest National Laboratory
> >> Richland, WA   USA
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide! 
> >> http://www.R-project.org/posting-guide.html
> >>
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> > 
>




More information about the R-help mailing list