Accessing items in a list of lists

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing items in a list of lists

nalbicelli
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}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing items in a list of lists

Henrique Dallazuanna
Try this:

do.call(rbind, lapply(comb, '[', 'a'))


On Wed, May 14, 2008 at 4:40 PM, <[hidden email]>
wrote:

> 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}}
>
> ______________________________________________
> [hidden email] 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing items in a list of lists

Sundar Dorai-Raj
In reply to this post by nalbicelli
[hidden email] said the following on 5/14/2008
12:40 PM:

> 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.

Try

data.frame(names.comb = names(comb),
            finaldata = sapply(comb, "[[", "a"))

HTH,

--sundar

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing items in a list of lists

Tony Plate-3
In reply to this post by nalbicelli
Try this:

 > 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)
 > sapply(comb, "[[", "a")
data1 data2 data3
     1     4     3
 > # Also, this can be useful:
 > comb[[c("data2", "b")]]
[1] 5
 >

[hidden email] wrote:

> 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}}
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing items in a list of lists

Dimitris Rizopoulos
In reply to this post by nalbicelli
check the following:

sapply(comb, "[[", "a")
# or
data.frame(
     "names" = names(comb),
     "value" = sapply(comb, "[[", "a"),
     row.names = seq_along(comb)
)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting [hidden email]:

> 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}}
>
> ______________________________________________
> [hidden email] 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.
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] 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.