Quantcast

Extract rows from a list object

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Extract rows from a list object

Vladimir Mikryukov
Dear list members,

I need to create a table from a huge list object,
this list consists of matrices of the same size (but with different
content).

The resulting n tables should contain the same rows from all matrices.

For example:
n <- 23
x <- array(1:20, dim=c(n,6))

huge.list <- list()
for (i in 1:1000) {
    huge.list[[i]] <- x }


# One of 1000 matrices
huge.list[[1]][1:4, 1:6]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    4    7   10   13   16
[2,]    2    5    8   11   14   17
[3,]    3    6    9   12   15   18
[4,]    4    7   10   13   16   19
...

# The result should be like that:
# One of n tables (with the row 4 from all 1000 matrices):
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    4    7   10   13   16   19
[2,]    4    7   10   13   16   19
[3,]    4    7   10   13   16   19
[4,]    4    7   10   13   16   19
...
[999,]    4    7   10   13   16   19
[1000,]    4    7   10   13   16   19


# I tried to convert a list object to an array
ARR <- array(unlist(huge.list), dim = c(dim(huge.list[[1]]),
length(huge.list)))
# then split it and use abind function, but it didn't work

Thanks in advance!
Vladimir

--
Vladimir Mikryukov
PhD student
 Institute of Plant & Animal Ecology UD RAS,
Lab. of Population and Community Ecotoxicology
[8 Marta 202, 620144, Ekaterinburg, Russia]
Tel.  +7 343 210 38 58 (ext.290)
Fax: +7 343 260 82 56, +7 343 260 65 00

        [[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
|  
Report Content as Inappropriate

Re: Extract rows from a list object

D. Rizopoulos
try something like this:

n <- 23
x <- array(1:20, dim = c(n, 6))
huge.list <- rep(list(x), 1000)

out <- lapply(1:4, function (i) {
     t(sapply(huge.list, "[", i = i, j = 1:6))
})

out[[1]]
out[[4]]


I hope it helps.

Best,
Dimitris


On 8/24/2010 11:29 AM, Vladimir Mikryukov wrote:

> Dear list members,
>
> I need to create a table from a huge list object,
> this list consists of matrices of the same size (but with different
> content).
>
> The resulting n tables should contain the same rows from all matrices.
>
> For example:
> n<- 23
> x<- array(1:20, dim=c(n,6))
>
> huge.list<- list()
> for (i in 1:1000) {
>      huge.list[[i]]<- x }
>
>
> # One of 1000 matrices
> huge.list[[1]][1:4, 1:6]
>       [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    1    4    7   10   13   16
> [2,]    2    5    8   11   14   17
> [3,]    3    6    9   12   15   18
> [4,]    4    7   10   13   16   19
> ...
>
> # The result should be like that:
> # One of n tables (with the row 4 from all 1000 matrices):
>       [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    4    7   10   13   16   19
> [2,]    4    7   10   13   16   19
> [3,]    4    7   10   13   16   19
> [4,]    4    7   10   13   16   19
> ...
> [999,]    4    7   10   13   16   19
> [1000,]    4    7   10   13   16   19
>
>
> # I tried to convert a list object to an array
> ARR<- array(unlist(huge.list), dim = c(dim(huge.list[[1]]),
> length(huge.list)))
> # then split it and use abind function, but it didn't work
>
> Thanks in advance!
> Vladimir
>
> --
> Vladimir Mikryukov
> PhD student
>   Institute of Plant&  Animal Ecology UD RAS,
> Lab. of Population and Community Ecotoxicology
> [8 Marta 202, 620144, Ekaterinburg, Russia]
> Tel.  +7 343 210 38 58 (ext.290)
> Fax: +7 343 260 82 56, +7 343 260 65 00
>
> [[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.
>

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
[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
|  
Report Content as Inappropriate

Re: Extract rows from a list object

Peter Dalgaard-2
In reply to this post by Vladimir Mikryukov

On Aug 24, 2010, at 11:29 AM, Vladimir Mikryukov wrote:

> Dear list members,
>
> I need to create a table from a huge list object,
> this list consists of matrices of the same size (but with different
> content).
>
> The resulting n tables should contain the same rows from all matrices.
>
> For example:
> n <- 23
> x <- array(1:20, dim=c(n,6))
>
> huge.list <- list()
> for (i in 1:1000) {
>    huge.list[[i]] <- x }
>
>
> # One of 1000 matrices
> huge.list[[1]][1:4, 1:6]
>     [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    1    4    7   10   13   16
> [2,]    2    5    8   11   14   17
> [3,]    3    6    9   12   15   18
> [4,]    4    7   10   13   16   19
> ...
>
> # The result should be like that:
> # One of n tables (with the row 4 from all 1000 matrices):
>     [,1] [,2] [,3] [,4] [,5] [,6]
> [1,]    4    7   10   13   16   19
> [2,]    4    7   10   13   16   19
> [3,]    4    7   10   13   16   19
> [4,]    4    7   10   13   16   19
> ...
> [999,]    4    7   10   13   16   19
> [1000,]    4    7   10   13   16   19
>
>
> # I tried to convert a list object to an array
> ARR <- array(unlist(huge.list), dim = c(dim(huge.list[[1]]),
> length(huge.list)))
> # then split it and use abind function, but it didn't work

You need to look in the direction of lapply() & friends

do.call(rbind,lapply(huge.list,"[",4,))

t(sapply(huge.list,"[",4,TRUE))

both seem to cut the mustard. (Notice that sapply() will cbind() the results automagically, and that for some odd reason it is more unhappy about missing arguments than lapply is.)

For more intelligible and generalizable code, also consider

do.call(rbind, lapply(huge.list, function(x)x[4,]))


--
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[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
|  
Report Content as Inappropriate

Re: Extract rows from a list object

Vladimir Mikryukov
In reply to this post by D. Rizopoulos
Thanks a lot!! It sure helped.
and many thanks to all other repliers!


On Tue, Aug 24, 2010 at 3:40 PM, Dimitris Rizopoulos <
[hidden email]> wrote:

> try something like this:
>
> n <- 23
> x <- array(1:20, dim = c(n, 6))
> huge.list <- rep(list(x), 1000)
>
> out <- lapply(1:4, function (i) {
>    t(sapply(huge.list, "[", i = i, j = 1:6))
> })
>
> out[[1]]
> out[[4]]
>

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