[R] Accessing items in a list of lists

From: <nalbicelli_at_tricadiacdpcmanagement.com>
Date: Wed, 14 May 2008 15:40:30 -0400

Using R 2.6.2, say I have the following list of lists, "comb":

data1 <- list(a = 1, b = 2, c = 3)
data2 <- list(a = 4, b = 5, c = 6)
data3 <- list(a = 3, b = 6, c = 9)

comb <- list(data1 = data1, data2 = data2, data3 = data3)

So that all names for the lowest level list are common. How can I most efficiently access all of the sublist items "a" indexed by the outer list names? For example, I can loop through comb[[i]], unlisting as I go, and then look up the field "a", as below, but there has got to be a cleaner way.

finaldata <- double(0)
for(i in 1:length(names(comb))) {

	test <- unlist(comb[[i]])
	finaldata <- c(finaldata, test[which(names(test) == "a")])
data.frame(names(comb), finaldata)

Gives what I want:
  names.comb. finaldata

1       data1         1
2       data2         4
3       data3         3

Any help you can give would be greatly appreciated. Thanks.

This information is being sent at the recipient's reques...{{dropped:16}}

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 14 May 2008 - 21:25:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 14 May 2008 - 23:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive