[R] Extracting multiple elements from a list

2004-01-15 Thread Waichler, Scott R
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

RE: [R] Extracting multiple elements from a list

2004-01-15 Thread Liaw, Andy
Aren't sapply()/lapply() sufficient for this? sapply(alist, function(x) x$vec) [,1] [,2] [1,]15 [2,]26 [3,]37 [4,]48 lapply(alist, function(x) x$vec) [[1]] [1] 1 2 3 4 [[2]] [1] 5 6 7 8 HTH, Andy From: Waichler, Scott R For a long time I've wanted a

RE: [R] Extracting multiple elements from a list

2004-01-15 Thread Prof Brian Ripley
[] works on a list and extracts multiple elements. However, that is not it seems what you want, rather, unions of elements of elements of lists. That is a pretty unusual need, and perhaps you could explain you you need it. Andy's solution still need something like do.call(c, lapply(alist,

Re: [R] Extracting multiple elements from a list

2004-01-15 Thread 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,

Re: [R] Extracting multiple elements from a list

2004-01-15 Thread Roger D. Peng
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:

RE: [R] Extracting multiple elements from a list

2004-01-15 Thread Waichler, Scott R
]] [[2]][[2]]$name [1] two [[2]][[2]]$vec [1] 5 6 7 8 -Original Message- From: Roger D. Peng [mailto:[EMAIL PROTECTED] Sent: Thursday, January 15, 2004 12:24 PM To: Waichler, Scott R Cc: [EMAIL PROTECTED] Subject: Re: [R] Extracting multiple elements from a list Sorry

Re: [R] Extracting multiple elements from a list

2004-01-15 Thread Julian Taylor
Waichler, Scott R wrote: 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