The tidyr
package in R helps create tidy data, providing different build-in functions used for data cleaning.
gather()
is used to gather multiple columns and collapse them into key-value pairs. It is invoked from the tidyr
package with different argument values, as shown below.
In the graphical illustration above, we've gathered the cinnamon_1
, cinnamon_2,
and nutmeg_3
columns into key and values pairs. We created a new column,
gather(data, key, value, ….)
It takes the following argument values.
data
: The name of the DataFrame.key
: The name of the key column that is to be created.value
: The name of the value column that is to be created.…
: This specifies the column from which the key-value pair will be gathered.It returns merged information of the same type as data
(an argument value).
Let's look at an example of the gather()
function below:
# demo program to show the working of gather()# importing tidyr librarylibrary("tidyr")# creating a DataFramedf <- data.frame(players=c('Amber', 'Paisley', 'Roxanne', 'Scarlett'),Year_2012=c(12, 5, 7, 19),Year_2013=c(15, 25, NA, 29))cat("Before Gather() Called\n")#viewing data frameprint(df)cat("\nAfter Gather() Called\n")# gathering the data from columns 2 and 3gather(df, key="Year", value="Matches Played", 2:3)
gather()
function is called.gather()
function will combine the players as the key and Year_2020, Year_2022 as values. Finally, we print the new DataFrame on the console.