title: Generate new column in list assigning different values by data frame from an array with lapply (R)
body: <p>I have a list (<em>lst</em>) of 3 data frames, each of which includes the column (<em>GDP</em>), and an array (<em>countries</em>) with the names of 3 countries (<em>ARG, IT, US</em>), as follows:</p>
  
  <pre><code>lst &lt;- list(data.frame(GDP=101:104), data.frame(GDP=1001:1003), data.frame(GDP=500:505));
  
  countries &lt;- array(c("ARG", "IT", "US"));
  </code></pre>
  
  <p>I want to create a new variable named <em>country</em> to be incorporated in the list so that the values from the array (<em>ARG, IT, US</em>) are "matched" to each data frame in the list.</p>
  
  <p>The 3 steps I followed so far are: </p>
  
  <ol>
  <li>Rename data frames included in <em>lst</em>:</li>
  </ol>
  
  <blockquote>
    <p>names(lst) &lt;- countries;</p>
  </blockquote>
  
  <ol start="2">
  <li>Function to loop through with <strong>lapply</strong> and assign country names:</li>
  </ol>
  
  <blockquote>
    <p>countryvar &lt;- function(x) {
         x$country &lt;- countries[i]
         return(x)
       };</p>
  </blockquote>
  
  <ol start="3">
  <li>Loop through the generated function <em>countryvar</em> with <strong>lapply</strong>:</li>
  </ol>
  
  <blockquote>
    <p>lst &lt;- lapply(lst, countryvar);</p>
  </blockquote>
  
  <p>My approach definitely does not work, as the new variable <em>country</em> is populated by NAs, as shown when running the following command: </p>
  
  <blockquote>
    <p>str(lst);</p>
  </blockquote>
  
  <p>I guess I might not be indexing the values included in the array correctly?</p>
  
  <p>Could anyone provide an efficient approach to solve this issue (ideally using <strong>lapply</strong>)? </p>