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
Extracting multiple elements from a list
3 messages · Waichler, Scott R, Roger D. Peng
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
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