# Extract elements of a list into a matrix?

I have a list with 9 elements.

``````mylist <- as.list(1:9)
mylist[] <- seq(1,3)
mylist[] <- seq(4,6)
mylist[] <- seq(7,9)
mylist[] <- seq(10,12)
mylist[] <- seq(13,15)
mylist[] <- seq(16,18)
mylist[] <- seq(19,21)
mylist[] <- seq(22,24)
mylist[] <- seq(25,27)
`````` These correspond to three 3x3 matrices, but how can I extract these numbers and fill in my matrices?

I want to create the three 3x3 matrices filled in with the corresponding number from each list element....like `mylist[]` 's first number is 1. So the first square in my matrix#1 should be 1.

I tried typing a description of how I want to fill in the squares but I think it was too confusing, so I'll just paste a manual representation I made of what I'm trying to achieve: Any ideas? Thank you!

EDIT

I had some success by basically taking the approach "extract the 1st number of all list elements, and dump into `mymatrix_1` - then just make the dimension 3x3 and transpose it.

Can I make this into a function so it does this for all 3?

``````#Extract the 1st number of each list element and dump into matrix#1
mymatrix_1 <- as.matrix(lapply(mylist, `[[`, 1))
dim(mymatrix_1) <- c(3,3)
mymatrix_1 <- t(mymatrix_1)

mymatrix_1
[,1] [,2] [,3]
[1,] 1    4    7
[2,] 10   13   16
[3,] 19   22   25
``````
``````mylist <- as.list(1:9)
mylist[] <- seq(1,3)
mylist[] <- seq(4,6)
mylist[] <- seq(7,9)
mylist[] <- seq(10,12)
mylist[] <- seq(13,15)
mylist[] <- seq(16,18)
mylist[] <- seq(19,21)
mylist[] <- seq(22,24)
mylist[] <- seq(25,27)

(initial <- array(unlist(mylist),dim = c(3,3,3)))

library(tidyverse)
library(glue)

do_inner <- function(x){
map(1:3,~initial[,,.][x,]) %>%
unlist %>%
matrix(nrow=3,ncol=3) %>% t()
}

1:3 %>%
set_names(~glue("Final Matrix {.}"))%>%
map(do_inner)``````
1 Like

Hmm, the last line seems to produce this error for me:

`Error: \$ operator is invalid for atomic vectors`

Is this because of `set_names` ?

do you get an error when you

``````1:3 %>%
map(do_inner)
``````

?

yes that's where I got the error.

However, I just restarted my R session and now it's working. Does this mean some other library was masking one of these functions or something?

Anyway, I checked and the matrices are correct! Thank you a ton!

@nirgrahamuk

Can't believe I got stuck with the dreaded "cannot allocate vector size" error I applied this example to a larger data set - it's making 16 12x12 matrices.

I didn't think this should be too big to handle at all. Do you think something might be wrong on my part, or is this truly too big? 12x12 means that the list has 144 elements. And in each element there are 16 values. So that's where I'm getting my number of 16 matrices each of dimensions 12x12.

The problem happens at this line:
`(initial <- array(unlist(mylist),dim = c(12,12,12,12,12,12,12,12,12,12,12,12)))`

`Error: cannot allocate vector of size 66430.1 Gb`

This was working so perfectly...is there anything I can do to fix it?

I think you just dont have the dimensions correct.
3 3x3 matrices is
dim(3,3,3)

3 12x12 matrices is
dim(12,12,3)

16 12X12 matrices is
dim(12,12,16)

I played around with this a few times, but I think this is just a little off.

I definitely got the dimensions wrong, but 16 12x12 matrices still required `dim = c(16,16,16)` and later in the `do_inner` function specifying 12x12 dimensions.

So this worked: (produced 16 12x12 matrices)

``````(initial <- array(unlist(mylist),dim = c(16,16,16)))

do_inner <- function(x){
map(1:12,~initial[,,.][x,]) %>%
unlist %>%
matrix(nrow=12,ncol=12) %>% t()
}

my_results <- 1:16 %>%
set_names(~glue("Final_Matrix_{.}"))%>%
map(do_inner)
``````

But this did not work: (produced 12 12x12 matrices)

``````(initial <- array(unlist(mylist),dim = c(12,12,16)))

do_inner <- function(x){
map(1:12,~initial[,,.][x,]) %>%
unlist %>%
matrix(nrow=12,ncol=12) %>% t()
}

my_results <- 1:12 %>%  #NOTE: cannot make this 1:16 or else get a 'subscript out of bounds' error
set_names(~glue("Final_Matrix_{.}"))%>%
map(do_inner)``````

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.