What is tidyr gather function in R?

If we need to gather a key-value pair from the specified columns then the gather() function in tidyr is used. It is deliberated as one of the core methods in R language. gather() is used when any column is not variable. Thus, it is quite a useful function for creating tidy data.

gather() function in R is used to merge multiple columns of the same values into a single while ungroup() function is used exactly in the opposite meanings.

Syntax

gather(data, key value, …)

Parameters

  • data: It contains the name of the data frame.
  • key: It shows the name of the key column which is to be created.
  • value: It depicts the name of the value column to be created.
  • : It shows the columns we need to gather the key-value pair.

Example 1

We are going to discuss an example regarding the gather() method.

#creating a data frame
df <- data.frame(Candidate=c('John', 'Topley', 'Brook', 'Smith','Miley'),
                 Maths=c(55, 84, 86, 99,100),
                 Stats=c(99, 85, 78, 98,79))
#printing data frame
print(df)
library(tidyr)
#gathering data across columns 2nd and 3rd
gather(df, key="Subject", value="Marks", 2:3)
  • Line#2-4: here we have created a data frame df.
  • Line#6: print the data frame df on the console.
  • Line#7: here we have called the tidyr library.
  • Line#9: it shows the usage of the gather() function to create key-value pairs by using 2nd and 3rd columns.
gather function in R output
gather function in R output

Example 2

Now we are going to discuss the example where we will gather values by using more than two columns.

#creating a data frame
df <- data.frame(Candidate=c('John', 'Topley', 'Brook', 'Smith','Miley'),
                 Maths=c(55, 84, 86, 99,100),
                 Stats=c(99, 85, 78, 98,79),
                 Economics=c(45,67,87,75,68))
#printing data frame
print(df)
library(tidyr)
#gathering data across columns 2nd and 3rd
gather(df, key="Subject", value="Marks", 2:4)

  • Line#2-5: here, we have created a data frame df.
  • Line#7: printing data frame df on the console.
  • Line#8: here we have called the tidyr library.
  • Line#10: shows the use of the gather() function to create key-value pairs using the 2nd, 3rd, and 4th columns. Then two new columns, namely Subject and Marks, are created by gathering the values of 2, 3, and 4 columns.
gather function in R
gather function in R output

Stay in the Loop

Get the daily email from Algoideas that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement -

You might also like...