Preparing your data for analysis with the tidyr package
The tidyr
package is another gift from Hadley Wickham. This package provides functions to make your data tidy.
This means that after applying the tidyr
package's function, your data you will be arranged as per the following rules:
- Each column will contain an attribute
- Each row will contain an observation
- Each cell will contain a value
These rules will produce a dataset similar to the following one:
This structure, besides giving you a clearer understanding of your data, will let you work with it more easily.
Furthermore, this structure will let you take full advantage of the inner R-vectorized structure. This recipe will show you how to apply the gather
function to a dataset in order to transform a dataset and make it comply with the cited rules.
The employed data frame is in the so-called wide format, where each period of observation is stored in columns, with each column representing a year, as follows:
Getting ready
In order to let...