0

I need help with extracting array from list.

array1 <- array(c(1,2,3), dim = c(3,3,3))
array2 <- array(c(10,20,30), dim = c(3,3,3))
A <- list(array1,array2)
B <- A[1]

"A" is list of two arrays. When I want to extract for example first array and assign it to "B", B isn't array, but it is also list. How can I access data stored in B? B[1,1,1] doesn't work.

  • 1
    https://stackoverflow.com/questions/1169456/the-difference-between-and-notations-for-accessing-the-elements-of-a-lis – jogo May 03 '18 at 12:47

2 Answers2

1

Use [[ to extract element in a list. If you use [, the output would still be a list.

array1 <- array(c(1,2,3), dim = c(3,3,3))
array2 <- array(c(10,20,30), dim = c(3,3,3))
A <- list(array1,array2)
B <- A[[1]]
www
  • 38,575
  • 12
  • 48
  • 84
0

If we are using magrittr, then extract2 can be used for the same purpose

library(magrittr)
B <- A %>% 
       extract2(1)
akrun
  • 874,273
  • 37
  • 540
  • 662