Home > Software design >  Subtracting two nested list of arrays using a for loop in R
Subtracting two nested list of arrays using a for loop in R

Time:10-16

I have a long list of nested arrays (60 elements long) and I'd like to subtract another nested list of arrays (also 60 elements long). The issue here is that elements in the first list have different dimensions than the second. Here's a condensed/simplified example. I have lists similar to example1 and example2 but mine are all different (non-repetitive) numbers:

#create example1
set.seed(1)
example1.1 <- list(array(rnorm(18), dim = c(6,3, 10)))
example1.2 <- list(array(rnorm(12), dim = c(4,3, 10)))
example1 <- list(example1.1, example1.2)

#create example2
set.seed(2)
example2.1 <- list(array(rnorm(18), dim = c(6,3, 1)))
example2.2 <- list(array(rnorm(12), dim = c(4,3, 1)))
example2 <- list(example2.1, example2.2)

Basically, I'd like to subtract example2.1 from example1.1 at each of it's different height positions, and then example2.2 from example1.2. It would be great if the output was also a list of arrays like:

[[1]]
[[1]][[1]]
, , 1

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 2

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 3

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 4

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 5

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 6

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 7

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 8

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 9

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

, , 10

            [,1]       [,2]       [,3]
[1,]  0.27046074 -0.2205257 -0.2285452
[2,] -0.00120586  0.9780227 -1.1750309
[3,] -2.42347394 -1.4086926 -0.6572980
[4,]  2.72565648 -0.1666014  2.2661355
[5,]  0.40975953  1.0941304 -0.8947948
[6,] -0.95288867 -0.5919095  0.9080295

[[2]]
[[2]][[1]]
, , 1

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 2

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 3

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 4

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 5

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 6

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 7

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 8

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 9

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

, , 10

           [,1]       [,2]       [,3]
[1,] -1.6392825 -1.2601304 0.09854405
[2,] -0.2486218 -2.7751200 0.29116978
[3,] -2.9264478  0.4824913 0.71957790
[4,]  2.7952066  3.1900311 0.10020653

I suspect I simply don't know how to subset the loop appropriately. Any help anyone can give is very much appreciated! Thank you!

CodePudding user response:

Maybe something like this ? :

Results.1 <- list(array(NA, dim = c(6,3,10)))
for(i in 1:10){
  Results.1[[1]][,,i] <- example1.1[[1]][,,i]-example2.1[[1]][,,1]
}

For the example1 list :

Results[[1]] = list(array(NA, dim = c(6,3, 10)))
Results[[2]] = list(array(NA, dim = c(4,3, 10)))
for(i in 1:length(example1)){
   for(j in 1:10){
     Results[[i]][[1]][,,j] <- example1[[i]][[1]][,,j]-example2[[i]][[1]][,,1]
     }
 }
  • Related