RE: [R] Extracting multiple elements from a list
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 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 -- Notice: This e-mail message, together with any attachments,...{{dropped}} __ [EMAIL PROTECTED] 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
RE: [R] Extracting multiple elements from a list
[] 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, function(x) x$vec)) but then it *is* unions of elements of elements of lists. On Thu, 15 Jan 2004, Liaw, Andy wrote: 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 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 -- Notice: This e-mail message, together with any attachments,...{{dropped}} __ [EMAIL PROTECTED] 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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] 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
Re: [R] Extracting multiple elements from a list
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 __ [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] 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
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 __ [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] 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
RE: [R] Extracting multiple elements from a list
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:[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, 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 __ [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] 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 __ [EMAIL PROTECTED] 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
Re: [R] Extracting multiple elements from a list
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 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 functions sapply() and lapply() have more generality that has been overlooked in this thread. This will answer your first question. unlist(lapply(alist, function(x, ind = c(2,4)) x$vec[ind])) [1] 2 4 6 8 hth, Julian -- --- Julian Taylor phone: +61 8 8303 6751 ARC Research Associatefax: +61 8 8303 6760 BiometricsSA, mobile: +61 4 1638 8180 University of Adelaide/SARDIemail: [EMAIL PROTECTED] Private Mail Bag 1www: http://www.BiometricsSA.adelaide.edu.au Glen Osmond SA 5064 There is no spoon. -- Orphan boy --- __ [EMAIL PROTECTED] 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