<p>After spending a day the other week struggling to make sense of a federal data set shared in an archaic format (ASCII fixed format <code>dat</code> file). </p><p>It is essential for the effective distribution and sharing of data that it use the minimum amount of disk space and be rapidly accessible for use by potential users. </p><p>In this post I test four different file formats available to R users. These formats are comma separated values <code>csv</code> (<code>write.csv()</code>), object representation format as a ASCII <code>txt</code> (<code>dput()</code>), a serialized R object (<code>saveRDS()</code>), and a Stata file (<code>write.dta()</code> from the <code>foreign</code> package). For reference, <code>rds</code> files seem to be identical to <code>Rdata</code> files except that they deal with only one object rather than potentially multiple.</p><p>In order to get an idea of how and where different formats outperformed each other I simulated a dataset composed of different common data formats. These formats were the following:</p><p><strong>Numeric Formats</strong></p><ul><li>Index 1 to N - ex. 1,2,3,4,...</li><li>Whole Numbers - ex. 30, 81, 73, 5, ...</li><li>Big Numbers - ex. 36374.989943, 15280.050850, 5.908210, 79.890601, 2.857904, ...</li><li>Continous Numbers - ex. 1.1681155, 1.6963295 0.8964436, -0.5227753, ...</li></ul><p><strong>Text Formats</strong></p><ul><li>String coded factor variables with 4 characters - ex. fdsg, jfkd, jfht, ejft, jfkd ...</li><li>String coded factor variables with 16 characters coded as strings</li><li>String coded factor variables with 64 characters coded as strings</li><li>Factor coded variables with 4 characters - ex. fdsg, jfkd, jfht, ejft, jfkd - coded as 1,2,4,3,2, ...</li><li>Factor coded variables with 16 characters</li><li>Factor coded variables with 64 characters</li><li>String variables with random 4 characters - ex. jdhd, jdjj, ienz, lsdk, ...</li><li>String variables with random 16 characters</li><li>String variables with random 64 characters</li></ul><p>What type of format a variable is in is a predictive characteristic for how much space that variable takes up and therefore how time consuming that variable is to read or write. For variables that are easy to describe they tend to take up little space. An index variable in an extreme example and can take up almost no space as it can be expressed in an extremely compact format (<code>1:N</code>). </p><p>In contrast numbers which are very long or have a great degree of precision tend to have more information and therefore take more resources to access and store. String variables when filled with truly random or unique responses are some of the hardest data to compress as each value may be sampled from the full character spectrum. There is some significant potential for compression when strings are repeated in the variable. These repetitive entries can be either coded as a &quot;factor&quot; variable or a string variable in R. </p><p>As part of this exploration, I look at how string data is stored and saved when coded as either a string or as a factor within R.</p><h1>Raw Files</h1><p>Let's first look at space taken when saving uncompressed files.</p><p><strong>Figure 1: File Size</strong></p><p></p><p>Figure 1 shows the file size of each of the saved variables when 10,000 observations are generated. The <code>dataframe</code> object is the <code>data.frame</code> composed off all of the variables. From the height of the <code>dataframe</code>, we can see that <code>rds</code> is overall the winner. Looking at the other variable values we can see only that <code>csv</code> appear to consistently underperform for most file formats except for random strings.</p><p><strong>Figure 2: File Size Log scaled</strong></p><p></p><p>In Figure 2 we can see that <code>rds</code> is consistently outperforming all of the other formats with the one exception of <code>index</code> in which the <code>txt</code> encoding simply reads <code>1:10000</code>. Apparently even serializing to bytes can't beat that.</p><p>Interestingly, there does not appear to be a effective size difference between repetitive strings encoded as factors accounting for the size of the strings (4, 16, or 64). We can see that the inability of <code>csv</code> to compress factor strings dramatically penalizes the efficiency of <code>csv</code> relative to the other formats. </p><h1>File Compression</h1><p>But data is rarely shared in uncompressed formats. How does compression change things?</p><p><strong>Figure 3: Zipped File Sizes Logged</strong></p><p></p><p>We can see from Figure 3 that if we zip our data after saving, the file size can do pretty much as well as <code>rds</code>. Comma delineated <code>csv</code> files are a bit of an exception with factor variables suffering under <code>csv</code>. Yet random strings perform slightly better under <code>csv</code> than other formats. Interesting <code>rds</code> files seem slightly larger than the other two file types. Overall though, it is pretty hard to see any significant difference in file size based on format after zipping.</p><p>So, should we stick with whatever format we prefer?</p><p>Not so fast. Sure, all of the files are similarly sized after zipping. This is useful for sharing files. But having to keep large file sizes on a hard drive is not ideal even if they can be compressed for distribution. There is finite space on any system and some files can be in the hundreds of MB to hundreds of GB range. Dealing with file formats and multiple file versions which are this large can easily drain the permanent storage capacity of most systems.</p><p>But an equally important concern, is how long it takes write and read different file formats. </p><h1>Reading Speed</h1><p>In order to test reading speeds, I loaded each of the different full dataframe files fifty times. I also tested how long it would take to unzip then load that file.</p><p><strong>Figure 4: Reading and unzipping average speeds</strong></p><p></p><p>From Figure 4, we can see that read speeds roughly correspond with the size of files. We can see that even a relatively small file (30 MB <code>csv</code> file) can take as long as 7 seconds to open. Working with large files saved in an inefficient format can be very frustrating.</p><p>In contrast, saving files in efficient formats can dramatically cut down on the time taken opening those files. Using the most efficient format (<code>rds</code>), files could be 100 times larger than those used in this simulation and still open in less than a minute.</p><h1>Conclusions</h1><p>Finding common file formats that any software can access is not easy. As a result many public data sets are provided in archaic formats which are poorly suited for end users.</p><p>This results in a wide pool of software sweets having the ability to access these datasets. However, with inefficient file formats comes a higher demand on the hardware of end users. I am unlikely to be the only person struggling with opening some of these large &quot;public access&quot; datasets.</p><p>Those maintaining these datasets will argue that sticking with the standard, inefficient format is the best of bad options. However, there is no reason they could not post datasets in <code>rds</code> formats in addition to the outdated formats they currently exist in. </p><p>And no we need not argue that selecting one software language to save data in will be biased toward those languages. Already many federal databases come with code supplements in Stata, SAS, or SPSS. To access these supplements, one is required to have paid access to that software. </p><p>Yet, R is free and its database format is public domain. Any user could download R, open a <code>rds</code> or <code>Rdata</code> file, then save that file in a format more suited to their purposes. None of these other proprietary database formats can boast the same.</p>