title: Convert list to dataframe in R and add column with names of sub-lists
body: <p>List <code>l</code> has three strings that are named one, two, and three, respectively. I want to convert <code>l</code> to a dataframe, and I need an additional column with the names in <code>n</code>.  </p>
  
  <pre><code>l &lt;- list(c("a", "b"), c("c", "d", "e"), c("e"))
  n &lt;- c("one", "two", "three")
  </code></pre>
  
  <p>I can do it using a loop, but I'm sure there are more efficient ways of doing this.</p>
  
  <pre><code>out &lt;- NULL
  for (i in 1:length(n)){
    step &lt;- rep(n[i], length(l[[i]]))
    out &lt;- c(out, step)}
  
  df &lt;- as.data.frame(unlist(l))
  df$n &lt;- out
  df
  
  #  unlist(l)     n
  #1         a   one
  #2         b   one
  #3         c   two
  #4         d   two
  #5         e   two
  #6         e three
  </code></pre>