title: R: add new column to a list of data frames with lapply
body: <p>i've read <a href="https://stackoverflow.com/questions/10150579/adding-a-column-to-a-data-frame/" title="this">this</a> and have created a lapply function to add a "SubCat" column to every data frame within a list. </p>
  
  <p>This is my code:</p>
  
  <pre><code>    my_list &lt;- lapply(1:length(my_list),     
                 function(i) cbind(my_list[[i]], my_list[[i]]["SubCat"] &lt;- as.character(""))) 
  </code></pre>
  
  <p>But get this error:</p>
  
  <pre><code>Error in `[&lt;-.data.frame`(`*tmp*`, "SubCat", value = "") : 
    replacement has 1 row, data has 0 
  </code></pre>
  
  <p>Whats wrong?</p>
  
  <p>When i use it, on a single data frame it works:</p>
  
  <pre><code>my_list[[1]]["SubCat"] &lt;- as.character("")
  </code></pre>
  
  <p>UPDATE: </p>
  
  <p>These are examples of my data frames, they all contain the same structure. One column for SKU and one for the Category.</p>
  
  <p>DataFrame 1:</p>
  
  <pre><code>    row.names       SKU         Tv.y.Video
  1   1699        2018143169254P  Tv.y.Video
  2   1700        2018143169254   Tv.y.Video
  3   1946        2018144678120P  Tv.y.Video
  4   1947        2018144678120   Tv.y.Video
  5   2366        2018146411831P  Tv.y.Video
  6   2367        2018146411831   Tv.y.Video
  </code></pre>
  
  <p>DataFrame 2:</p>
  
  <pre><code>    row.names   SKU             C�mputo
  1     6       2004121460000P    C�mputo
  2     7       2004121460000     C�mputo
  3     8       2004121440002P    C�mputo
  4     9       2004121440002     C�mputo
  5     10      2004123030003P    C�mputo
  6     11      2004123030003     C�mputo
  </code></pre>
  
  <p>When i applied my code to just one df it works:</p>
  
  <pre><code>my_list[[1]]["SubCat"] &lt;- as.character("")
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>    row.names       SKU         Tv.y.Video    SubCat
  1   1699        2018143169254P  Tv.y.Video   
  2   1700        2018143169254   Tv.y.Video   
  3   1946        2018144678120P  Tv.y.Video   
  4   1947        2018144678120   Tv.y.Video   
  5   2366        2018146411831P  Tv.y.Video   
  6   2367        2018146411831   Tv.y.Video   
  </code></pre>
  
  <p><strong><em>UPDATE 1:</em></strong></p>
  
  <p>I also have some empty data.frames in the list. </p>