title: How to dynamically list matrices as elements in a list in R
body: <p>This post is similar to my other <a href="https://stackoverflow.com/questions/63231431/sum-of-a-list-of-matrices-in-r/63231500#63231500">post</a></p>
  <p>Let's say if I have 4 matrices:</p>
  <pre><code>x1 &lt;- matrix(1:9, nrow = 3)
  x2 &lt;- matrix(2:10, nrow = 3)
  x3 &lt;- matrix(3:11, nrow = 3)
  x4 &lt;- matrix(4:12, nrow = 3)
  </code></pre>
  <p>And I want to put them into a <code>list()</code> in a way like this:</p>
  <pre><code>[[1]]
  [[1]][[1]]
       [,1] [,2] [,3]
  [1,]    1    4    7
  [2,]    2    5    8
  [3,]    3    6    9
  
  [[1]][[2]]
       [,1] [,2] [,3]
  [1,]    2    5    8
  [2,]    3    6    9
  [3,]    4    7   10
  
  
  [[2]]
  [[2]][[1]]
       [,1] [,2] [,3]
  [1,]    3    6    9
  [2,]    4    7   10
  [3,]    5    8   11
  
  
  [[3]]
  [[3]][[1]]
       [,1] [,2] [,3]
  [1,]    4    7   10
  [2,]    5    8   11
  [3,]    6    9   12
  </code></pre>
  <p>Given that I have this <code>list()</code> to define which element in <code>x</code> to list in each element in the <code>list</code>:</p>
  <pre><code>&gt; list &lt;- list(c(1,2),3,4)
  
  [[1]]
  [1] 1 2
  
  [[2]]
  [1] 3
  
  [[3]]
  [1] 4
  </code></pre>
  <p>Is there a way to write this list in a more dynamic way? Currently, I'm using this</p>
  <pre><code>x &lt;- list(list(x1,x2),list(x3),list(x4))
  </code></pre>
  <p>I have tried using the below codes but they don't work:</p>
  <pre><code>for(p in 1:3) {
    x &lt;- lapply(list, function(l) list(get(paste0(&quot;x&quot;,list[[p]]))))
  }
  </code></pre>
  <p>and</p>
  <pre><code>for(p in 1:3) {
    x &lt;- lapply(list, function(l) list(get(paste0(&quot;x&quot;,l))))
  }
  </code></pre>