This vignette focuses on what git2rdata does to make storing dataframes under version control more efficient and convenient. All details on the actual file format are described in vignette("plain_text", package = "git2rdata"). Hence we will not discuss the optimize and na arguments to the write_vc() function.

We will not illustrate the efficiency of write_vc() and read_vc() since that is covered in vignette("efficiency", package = "git2rdata").


A critical assumption made by git2rdata is that all information is contained within the dataframe itself. Each row is an observation, each column is a variable and only the variables are named. This implies that two observations switching place does not alter the information content. Nor does switching two variables.

Version control systems like git, subversion or mercurial focus on accurately keeping track of any change in the files. Two observations switching place in a plain text file is a change, although the information content1 doesn’t change. Therefore git2rdata helps the user to prepare the plain text files in such a way that any change in the version history is an actual change in the information content.

Sorting Observations

Version control systems often track changes in plain text files based on row based differences. In layman’s terms they only record which lines in a file are removed and which lines are inserted at what location. Changing an existing line implies removing the old version and inserting the new one. This is illustrated in the minimal example below.

Original version


Altered version. The row containing 1, 10 was moved to the last line. The row containing 3,12 was changed to 3,0


Diff between original and altered version. Notice than we have a deletion of two lines and two insertions.

Ensuring that the observations are always sorted in the same way thus helps minimizing the diff. The sorted version of the same altered version looks like the example below.


Diff between original and the sorted alternate version. Notice that all changes revert to actual changes in the information content. Another benefit is that changes are easily spotted in the diff. A deletion without insertion on the next line is a removed observation. An insertion without preceding deletion is a new observation. A deletion followed by an insertion is an updated observation.

This is where the sorting argument comes into play. If this argument is not provided when a file is written for the first time, it will yield a warning about the lack of sorting. The observations will be written in their current order. New versions of the file will not apply any sorting either, leaving this burden to the user. This is illustrated by the changed hash for the data file in the example below, whereas the metadata is not changed (no change in hash).

sorting should contain a vector of variable names. The observations are automatically sorted along these variables prior to writing. However, we now get an error because the set of sorting variables has changed. The set of sorting variables is stored in the metadata. Changing the sorting can potentially lead to large diffs, which git2rdata tries to avoid as much as possible.

From this moment on we will store the output of write_vc() in an object to minimize the output.

Using strict = FALSE turns such errors into warnings and allows to update the file. Notice that we get a new warning: the variable we used for sorting resulted in ties, thus the order of the observations is not guaranteed to be stable. The solution is to use more or different variables. We’ll need strict = FALSE again to override the change in sorting variables.

Once the sorting is defined we may omit the sorting argument when writing new versions. The sorting as defined in the existing metadata will be used to sort the observations. A check for potential ties will be performed and results in a warning when ties are found.

Sorting Variables

The order of the variables (columns) has an even bigger impact on a row based diff. Let’s revisit our minimal example. Suppose that we swap A and B from our original example. The new data looks as below.


The resulting diff is maximal because every single row was updated. Yet none of the information was changed. Hence, it is crucial to maintain column order when storing dataframes as plain text files under version control. This is illustrated on a more realistic data set in the vignette("efficiency", package = "git2rdata") vignette.

git2rdata tackles this problem by storing the order of the columns in the metadata. The order is defined by the order in the dataframe when it is written for the first time. From that moment on, the same order will be reused. The example below writes the same data set twice. The second version contains exactly the same information but randomizes the order of the observations and the columns. The sorting by the internals of write_vc() will undo this randomization, resulting in an unchanged file.

Handling Factors Optimized

vignette("plain_text", package = "git2rdata") and vignette("efficiency", package = "git2rdata") illustrate how a factor can be stored more efficiently when storing their index in the data file and the indices and labels in the metadata. We take this even a bit further: what happens if new data arrives and an extra factor level is required?

Let’s add an observation with a new factor level. If we store the updated dataframe in a new file, we see that the indices are different. The factor level "blue" remains unchanged, but "red" becomes the third level and get index 3 instead of index 2. This could lead to a large diff whereas the potential semantics (and thus the information content) are not changed.

When we try to overwrite the original data with the updated version, we get an error because there is a change in factor levels and / or indices. In this specific case, we decided that the change is OK and force the writing by setting strict = FALSE. Notice that the original labels ("blue" and "red") keep their index, the new level ("green") gets the first available index number.

The next example removes the "blue" level and switches the order of the remaining levels. Notice that again the existing indices are retained. The order of the labels and indices reflects their new ordering.

Changing a factor to an ordered factor or vice versa will also keep existing level indices.

Relabelling a Factor

The example below will store a dataframe, relabel the factor levels and store it again using write_vc(). Notice that both the labels and the indices are updated. Hence creating a large diff, where just updating the labels would be sufficient.

Therefore we created relabel(), which changes only the labels in the metadata. It takes three arguments: the name of the data file, the root and the change. change accepts two formats, a list or a dataframe. The name of the list must match with the variable name of a factor in the data. Each element of the list must be a named vector, the name being the existing label and the value the new label. The dataframe format requires a factor, old and new variable with one row for each change in label.

A caveat: relabel() only makes sense when the data file uses optimized storage. The verbose mode stores the factor labels and not their indices, in which case relabelling a label will always yield a large diff. Therefore relabel() will only handle the optimized storage.

  1. sensu git2rdata