Adding Comments to CSV Files

January 11, 2013

Various of my R scripts produce csv files as output. For instance, I run a lengthy SVM back test, the end result is a csv file containing the indicator with some additional information. The problem is that over time one loses track what exactly the file contained and what parameters were used to produce it.

I have considered various solutions. First, I do store some information in the file name. However, this has limited applications. Next I considered using file system attributes (getfattr, setfattr, etc). Although an exciting feature, it doesn’t seem to integrate easily with git.

The solution I ended up using is to add comments to the csv file. The implementation is surprisingly easy. R’s read.table supports comments starting ‘#’ by default. I only needed to create a decent function to write the comments:

writeIndicator = function( indicator, fileName, comments=c() )
   require( quantmod, quietly=TRUE )

   # First write the comments
   append = FALSE
   for( comment in comments ) { 
      line = paste( sep="", "# ", comment )
      write( line, file=fileName, append=append )
      append = TRUE

   # Write the series, but suppress warnings to avoid the following:
   #    Warning message:
   #    In write.table(dx, file = file, row.names = row.names, col.names = col.names,  :
   #    appending column names to file
   suppressWarnings( write.zoo( indicator, quote=F, row.names=F, sep=",", file=fileName, append=append ) ) 

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.