I have a list with 9 elements.
mylist <- as.list(1:9)
mylist[[1]] <- seq(1,3)
mylist[[2]] <- seq(4,6)
mylist[[3]] <- seq(7,9)
mylist[[4]] <- seq(10,12)
mylist[[5]] <- seq(13,15)
mylist[[6]] <- seq(16,18)
mylist[[7]] <- seq(19,21)
mylist[[8]] <- seq(22,24)
mylist[[9]] <- 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[[1]]
'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