title: R: Export list to file with individual lists as columns
body: <p>I have been struggling for some time to generate a specific output format using R, namely a <strong>tab-separated multi-list file, with each column representing one list and the first row containing the names of the individual lists</strong>.</p>
  
  <p>I have saved the individual lists in a list object:</p>
  
  <pre><code>multi_list &lt;- list(vector1=c(1,2,3),vector2=c(4,5),vector3=c(6,7,8,9),vector4=c(10)) 
  </code></pre>
  
  <p>My problem consists in the fact that the individual lists are of different lengths and thus cannot be concatenated as a data frame without adding <code>NA</code> values to the shorter lists which doesn’t work for the required output format.</p>
  
  <p>How can I write my multi-list to a tsv file with each vector as a separate column?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I found a solution based on the answer to <a href="https://stackoverflow.com/questions/27208354/in-r-write-vectors-of-different-length-into-cvs-file">this</a> question as suggested below. May not be the most elegant way but it works:</p>
  
  <pre><code>df &lt;- data.frame(vector1 = rep(NA, max(sapply(multi_list, length))))
  df[1:length(multi_list$vector1), 1] &lt;- multi_list$vector1
  df[1:length(multi_list$vector2), 2] &lt;- multi_list$vector2
  df[1:length(multi_list$vector3), 3] &lt;- multi_list$vector3
  df[1:length(multi_list$vector4), 4] &lt;- multi_list$vector4
  colnames(df) &lt;- c("vector1", "vector2", "vector3", "vector4")
  
  write.table(df, file = 'multi_list.tsv', row.names = FALSE, na = '', sep="\t")
  </code></pre>