title: add variable to a list in R
body: <p>I have 28 list within a list and I try to add another variable called ID to each individual list.  I found this <a href="https://stackoverflow.com/questions/7073785/dataframes-in-a-list-adding-a-new-variable-with-name-of-dataframe">Dataframes in a list; adding a new variable with name of dataframe</a> to be very helpful.  But when i tried his code, it doesn't work in my case.  I think it's because  my list doesn't have clear labels [1],[2].[3], etc.. that the code can recognize.</p>
  
  <pre><code>all$id &lt;- rep(names(mylist), sapply(mylist, nrow))
  
  
  &gt;List of 1
  $ :List of 28
  
    ..$ :'data.frame':    271 obs. of  12 variables:
  
  
    .. ..$ Sample_ID                 : Factor w/ 271 levels "MC25",..: 19 27 2
  
    .. ..$ Reported_Analyte          : Factor w/ 10 levels "2-Butoxyethanol",..: 7 7 7 
  
   ..$ Date_Collected            : Factor w/ 71 levels "2010-05-08","2010-05-09",..: 8 9 1
  
    .. ..$ Result2                   : num [1:271] 0.11 0.11 0.11 0.11 
  
    ..$ :'data.frame':    6 obs. of  12 variables:
  
  
    .. ..$ Sample_ID                 : Factor w/ 271 levels "MC25",..: 19 27 2
  
    .. ..$ Reported_Analyte          : Factor w/ 10 levels "2-Butoxyethanol",..: 7 7 7 
  
   ..$ Date_Collected            : Factor w/ 71 levels "2010-05-08","2010-05-09",..: 8 9 1
  
    .. ..$ Result2                   : num [1:271] 0.11 0.11 0.11 0.11 
  </code></pre>