3

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?

I am currently storing these labels in R as data frame attributes. A working example would be:

my.data <- data.frame(x = 1:10,y = 11:20)
attributes(my.data)$variable.labels <- c("numbers ranging from 1 to 10", 
                                         "numbers ranging from 11 to 20")

My current solution is saving the data and attributes as two separate files like so:

write.csv2(my.data, file = "my_data.csv")
write.csv2(attributes(my.data)$variable.labels, file = "my_data_attr.csv")

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.

I was thinking of trying to add metadata by inserting comments in .csv like suggested in this question Python: skip comment lines marked with # in csv.DictReader or trying use .xml like in this blog post (http://blog.mafr.de/2008/04/26/metadata-for-csv/), 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.

Community
  • 1
  • 1
Chris Novak
  • 143
  • 7
  • 1
    why don't you just add another row to your column with the meta data? How would you want the meta data to be shown in excel? in a cell? The question you shared is about how to leave out the meta data when writing it to a csv file. csv does not support meta data, it is either a seperate file or a field in your [spreadsheat](http://stackoverflow.com/questions/1961006/can-a-csv-file-have-a-comment) – Mehdi Nellen Mar 02 '15 at 17:43
  • Ideally I would like the meta data related to a variable to be shown in the spreadsheet under or over the variable cell. Better still as a comment to the cell that holds the variable name in the .xml file. – Chris Novak Mar 02 '15 at 23:18

0 Answers0