title: How to save variable labels in an external file in R
body: <p>While working on a project on data imported from .sav file I found that creating variable labels helped my workflow. I would like to store these labels in a file, together with the data itself, for future reference and collaboration. My question is if there is any way to do that without creating a separate file for the labels?</p>
  
  <p>I am currently storing these labels in R as data frame attributes. A working example would be:</p>
  
  <pre><code>my.data &lt;- data.frame(x = 1:10,y = 11:20)
  attributes(my.data)$variable.labels &lt;- c("numbers ranging from 1 to 10", 
                                           "numbers ranging from 11 to 20")
  </code></pre>
  
  <p>My current solution is saving the data and attributes as two separate files like so:</p>
  
  <pre><code>write.csv2(my.data, file = "my_data.csv")
  write.csv2(attributes(my.data)$variable.labels, file = "my_data_attr.csv")
  </code></pre>
  
  <p>The problem I am having with this approach is that saving and than loading two files simultaneously is cumbersome and error-prone. I understand I could try to save it in a .sav file, but I would like the file to be editable with an accessible with spreadsheet software like MS EXCEL or equivalent so the people with whom I collaborate can make corrections and additions.</p>
  
  <p>I was thinking of trying to add metadata by inserting comments in .csv like suggested in this question <a href="https://stackoverflow.com/questions/14158868/python-skip-comment-lines-marked-with-in-csv-dictreader">Python: skip comment lines marked with # in csv.DictReader</a> or trying use .xml like in this blog post (<a href="http://blog.mafr.de/2008/04/26/metadata-for-csv/" rel="nofollow noreferrer">http://blog.mafr.de/2008/04/26/metadata-for-csv/</a>), but I don't know how to implement any of these in R, and am not sure in what way it will be editable with a spreadsheet afterwards.</p>