title: Assign, Save and Load a Sequence of Objects to a Sequence of Variables in R
body: <p>I would like to assign a sequence of results to a sequence of variables and then save these variables in a sequence of .Rdata files. I want to do this in order to use sapply to load all the .Rdata files once I'll need them.<br />
  To illustrate my problem, I wrote a simple function <em>save.try1</em>, and ran it 5 times.</p>
  <pre><code>save.try1 = function(x){
  
  M = matrix(c(x,x+1,x+2,x+3), ncol=2, nrow = 2)
  
  filename = paste('/Volumes/GIORDANO 3/MTS/MTS - essai/matrice', x, '.Rdata', sep='')
  
  save(M, file =filename) 
  
  }
  
  sapply(1:5, save.try1)
  </code></pre>
  <p>I now have 5 .Rdata files named <em>matrice1.Rdata, ... , matrice5.Rdata</em> in my folder <em>MTS - essai</em>.</p>
  <p>Now, when I load the files in R, I obtain:</p>
  <pre><code>&gt; matrices.chargees = sapply(1:5, function(x) load(file = paste('/Volumes/GIORDANO 3/MTS/MTS - essai/matrice', x, '.Rdata', sep='')))
  &gt; matrices.chargees
  [1] &quot;M&quot; &quot;M&quot; &quot;M&quot; &quot;M&quot; &quot;M&quot;
  </code></pre>
  <p>And I can't open any of the <em>M</em> matrices:</p>
  <pre><code>&gt; matrices.chargees[[2]]
  [1] &quot;M&quot;
  &gt; matrices.chargees[2]
  [1] &quot;M&quot;
  &gt; matrices.chargees$M
  Error in matrices.chargees$M : $ operator is invalid for atomic vectors  
  </code></pre>
  <p>First, I want to have different names for each matrix. I read the following post:</p>
  <p><a href="https://stackoverflow.com/questions/19159881/how-to-assign-a-sequence-of-computations-to-a-sequence-of-variables-in-r">how to assign a sequence of computations to a sequence of variables in r</a></p>
  <p>I tried to change my code to:</p>
  <pre><code>save.try1 = function(x){
  
  M = matrix(c(x,x+1,x+2,x+3), ncol=2, nrow = 2)
  
  assign(paste('M', x, sep =''), M)
  
  filename = paste('/Volumes/GIORDANO 3/MTS/MTS - essai/matrice', x, '.Rdata', sep='')
  
  save(paste('M', x, sep =''), file =filename) 
  
  }  
  </code></pre>
  <p>But I obtain the following error:</p>
  <pre><code>&gt; sapply(1:5, save.try1)
  Error in save(paste(&quot;M&quot;, x, sep = &quot;&quot;), file = filename) : 
    object ‘paste(&quot;M&quot;, x, sep = &quot;&quot;)’ not found
  </code></pre>
  <p>My questions are therefore the following:</p>
  <p>1- how do I modify my code to have a different name for each matrix?
  2- Will this allow me to access the content of each matrix? If not, how do I access it?</p>
  <p>Thank you!</p>