Tidyr package is used to create tidy data. Tidy data is a standard way of storing data that is used to ensure that we spend less time fighting with the tools and more time working on the analysis.
The different functions of Tidyr package are described here.
Problem:
Use of Tidyr Gather() in R
Solution:
In some scenarios, the Data is unstacked and spread out across columns. In those scenarios, Gather() helps to reformat the data such that the column attributes are gathered together as a single variable.
Gather() function takes multiple columns and classifies them into two columns:
1. Key column – Contains the former column names
2. Value column – Contains the cells of the former columns
Let’s work on the famous iris dataset. The dataset looks like below:
Iris DataSet |
We can get the first observation of each Species from the iris DataSet and here goes the code:
new_iris |
On using the gather() function, the above dataset converts into key-value pairs like below: