The gather() function is helpful in R when working with dataframes’ columns. This function converts a range of columns’ labels and values into key and value columns.
Let’s move on to discover all about the function.
What is the gather() function in R?
The gather() function executes on a range of dataframe‘s columns to create a pair of keys and values from columns’ labels and values, respectively.
Syntax:
apply(data_frame, key_name, value_name, target_column)
Arguments:
- data_frame: A data frame to apply the gather() function.
- key_name: Key column’s name.
- value_name: Value column’s name.
- target_column: Range of columns to create key and value columns.
How to use the gather() function?
If you want to use the function, you have to determine 4 parameters: the dataframe’s name, that is, the dataframe to apply the function, the key and value column’s name, and a range of columns to create the pair of key and value.
Below are some examples of using the gather() function.
Use the gather() with two columns
In the example below, we pass the dataframe named data to apply the function. Key and value columns are named Key and Value, respectively. Finally, we pass the first and second columns by the colon “:”
Code:
# Import tidyr library to work with the gather() function library(tidyr) # Create three vectors named Name, Age, and State Name <- c("John", "David", "Donald", "James", "Evelyn", "Lily", "Daisy") Age <- c(22, 31, 17, 41, 53, 26, 27) State <- c("Florida", "Ohio", "Florida", "Texas", "Washington", "California", "Washington") # Create a data frame named data from the vectors data = data.frame(Name, Age, State) # Gather data from columns 1 and 2 result = gather(data, key="Key", value="Value", 1:2) print(result)
Result:
State Key Value
1 Florida Name John
2 Ohio Name David
3 Florida Name Donald
4 Texas Name James
5 Washington Name Evelyn
6 California Name Lily
7 Washington Name Daisy
8 Florida Age 22
9 Ohio Age 31
10 Florida Age 17
11 Texas Age 41
12 Washington Age 53
13 California Age 26
14 Washington Age 27
Use the gather() function with more than two columns
In this example, we will use the function to create a pair of keys and values from 4 columns.
Code:
# Import tidyr library to work with the gather() function library(tidyr) # Create three vectors named Name, Age, and State Name <- c("John", "David", "Donald", "James", "Evelyn", "Lily") Age <- c(22, 31, 17, 41, 53, 26) State <- c("Florida", "Ohio", "Florida", "Texas", "Washington", "California") Hair_color <- c("Yellow", "Black", "Brown", "Yellow", "Pink", "Yellow") # Create a data frame named data from the vectors data = data.frame(Name, Age, State, Hair_color) # Gather data from columns 1 to 4 result = gather(data, key="Key", value="Value", 1:4) print(result)
Result:
Key Value
1 Name John
2 Name David
3 Name Donald
4 Name James
5 Name Evelyn
6 Name Lily
7 Age 22
8 Age 31
9 Age 17
10 Age 41
11 Age 53
12 Age 26
13 State Florida
14 State Ohio
15 State Florida
16 State Texas
17 State Washington
18 State California
19 Hair_color Yellow
20 Hair_color Black
21 Hair_color Brown
22 Hair_color Yellow
23 Hair_color Pink
24 Hair_color Yellow
Summary
In summary, the gather() function transforms a range of columns in a dataframe into a new dataframe that contains 2 columns representing key and value columns. We hope this article will be helpful for you. Thank you for reading!
Maybe you are interested:
- The seq Function in R: How to use seq() in R
- The attach() Function In R: How To Use attach() In R
- lag function in R: How To Use Lag Function In R

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP