title: Subtraction of list of matrix within a list in R
body: <p>I have a <code>list</code> of which consists of <code>list</code> of matrices as follows:</p>
  <pre><code>$A
  $A[[1]]
  $A[[1]][[1]]
       [,1] [,2]
  [1,]    1    6
  [2,]    2    7
  [3,]    3    8
  [4,]    4    9
  [5,]    5   10
  
  
  $A[[2]]
  $A[[2]][[1]]
       [,1] [,2]
  [1,]    2    7
  [2,]    3    8
  [3,]    4    9
  [4,]    5   10
  [5,]    6   11
  
  
  
  $B
  $B[[1]]
  $B[[1]][[1]]
       [,1] [,2]
  [1,]   11   16
  [2,]   12   17
  [3,]   13   18
  [4,]   14   19
  [5,]   15   20
  
  
  $B[[2]]
  $B[[2]][[1]]
       [,1] [,2]
  [1,]   12   17
  [2,]   13   18
  [3,]   14   19
  [4,]   15   20
  [5,]   16   21
  
  
  
  $C
  $C[[1]]
  $C[[1]][[1]]
       [,1] [,2]
  [1,]    0    0
  [2,]    0    0
  [3,]    0    0
  [4,]    0    0
  [5,]    0    0
  
  
  $C[[2]]
  $C[[2]][[1]]
       [,1] [,2]
  [1,]    0    0
  [2,]    0    0
  [3,]    0    0
  [4,]    0    0
  [5,]    0    0
  </code></pre>
  <p>The above list of matrices was created using the codes below:</p>
  <pre><code>A_mat1 &lt;- matrix(as.numeric(c(1:10)), nrow = 5, ncol = 2)
  B_mat1 &lt;- matrix(as.numeric(c(11:20)), nrow = 5, ncol = 2)
  C_mat1 &lt;- matrix(as.numeric(0), nrow = 5, ncol = 2)
  A_mat2 &lt;- matrix(as.numeric(c(2:11)), nrow = 5, ncol = 2)
  B_mat2 &lt;- matrix(as.numeric(c(12:21)), nrow = 5, ncol = 2)
  C_mat2 &lt;- matrix(as.numeric(0), nrow = 5, ncol = 2)
  
  my_matrix_name &lt;- as.vector(c(&quot;A_mat1&quot;, &quot;B_mat1&quot;, &quot;C_mat1&quot;, &quot;A_mat2&quot;, &quot;B_mat2&quot;, &quot;C_mat2&quot;))
  my_list = list(A_mat1, B_mat1, C_mat1, A_mat2, B_mat2, C_mat2)
  names(my_list) &lt;- my_matrix_name
  
  my_A &lt;- as.vector(my_matrix_name[substring(my_matrix_name,1,1) == &quot;A&quot;])
  my_B &lt;- as.vector(my_matrix_name[substring(my_matrix_name,1,1) == &quot;B&quot;])
  my_C &lt;- as.vector(my_matrix_name[substring(my_matrix_name,1,1) == &quot;C&quot;])
  
  A = list()
  for(i in seq_len(length(my_A))){
    A[[i]] &lt;- list(my_list[[paste0(my_A[[i]])]])
  }
  
  B = list()
  for(i in seq_len(length(my_B))){
    B[[i]] &lt;- list(my_list[[paste0(my_B[[i]])]])
  }
  
  C = list()
  for(i in seq_len(length(my_C))){
    C[[i]] &lt;- list(my_list[[paste0(my_C[[i]])]])
  }
  
  my_group = list(A,B,C)
  names(my_group) &lt;- c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;)
  my_group
  </code></pre>
  <p>Where group <code>C</code> matrices have values of 0. Now I want to create a formula to overwrite group <code>C</code> equals to group <code>A</code> minus group <code>B</code> i.e. C = A - B</p>
  <p>I have tried using</p>
  <pre><code>my_group[[3]] = my_group[[1]] - my_group[[2]]
  </code></pre>
  <p>and</p>
  <pre><code>my_group[[3]] = Map(&quot;-&quot;, my_group[[1]] , my_group[[2]])
  </code></pre>
  <p>but it gives error message: non-numeric argument to binary operator. Can anyone help me please? Thanks!</p>