The unite() function in R

unite function in r

If you are looking for a method to merge data frame columns in R, look at this article. Today, we will introduce the unite() function in R – a helpful function to merge two or even more columns into a single column.

What is the unite() function in R?

The unite() function merges two data frame columns or more into a single one and returns a new data frame containing both the original and a new column.

Syntax: 

unite(data_frame, col, target_columns, sep, remove)

Parameters: 

  • data_frame: The dataframe to merge its columns.
  • col: The parameter to determine the name of the new column.
  • target_columns: The name of columns that are used to merge.
  • sep: The type of separation that is used for the data of the new columns.
  • remove: Boolean value. The default value is TRUE. Remove target columns in the new data frame.

How to use the unite() function?

Use the unite() function to merge two columns

First, we will learn how to merge two columns using the unite() function. We will create a data frame to store a few pieces of personal information. Then, use the unite() function to merge two columns.

Code:

library('tidyr')

# Create vectors
Name <- c("John", "David", "Donald", "James", "Evelyn", "Lily", "Daisy")
Day <- c(22, 13, 15, 24, 3, 5, 21)
Month <- c("June", "July", "March", "October", "December", "August", "May")
Year <- c(1990, 1995, 1998, 1977, 2001, 2004, 1988)
 
# Create a data frame from vectors
data <- data.frame(Name, Day, Month)
 
# Merge the column Day and Month into the column Birthday
result <- unite(data, col = "Birthday", Day, Month, sep='/')
 
# Print the new data frame after merging
print(result)

Result:

Result:
    Name     Birthday
1   John      22/June
2  David      13/July
3 Donald     15/March
4  James   24/October
5 Evelyn   3/December
6   Lily     5/August
7  Daisy       21/May

Use the unite() function to merge more than two columns

Similar to the example above, but in this part, we will merge three columns.

Code:

library('tidyr')

# Create vectors
Name <- c("John", "David", "Donald", "James", "Evelyn", "Lily", "Daisy")
Day <- c(22, 13, 15, 24, 3, 5, 21)
Month <- c("June", "July", "March", "October", "December", "August", "May")
Year <- c(1990, 1995, 1998, 1977, 2001, 2004, 1988)
 
# Create a data frame from vectors
data <- data.frame(Name, Day, Month, Year)
 
# Merge the three columns Day, Month, and Year into the column Birthday
result <- unite(data, col = "Birthday", Day, Month, Year, sep='/')
 
# Print the new data frame after merging
print(result)

Result:

    Name         Birthday
1   John     22/June/1990
2  David     13/July/1995
3 Donald    15/March/1998
4  James  24/October/1977
5 Evelyn  3/December/2001
6   Lily    5/August/2004
7  Daisy      21/May/1988

Use the unite() function to merge columns without removing the input columns

In the two examples above, we have shown you how to merge two and more two columns and use the default removal. In this example, we will keep the input columns and see what the new data frame looks like. Use the FALSE value for the remove parameter to tell the function to keep the input columns.

Code:

library('tidyr')

# Create vectors
Name <- c("John", "David", "Donald", "James", "Evelyn", "Lily", "Daisy")
Day <- c(22, 13, 15, 24, 3, 5, 21)
Month <- c("June", "July", "March", "October", "December", "August", "May")
Year <- c(1990, 1995, 1998, 1977, 2001, 2004, 1988)
 
# Create a data frame from vectors
data <- data.frame(Name, Day, Month, Year)
 
# Merge the three columns Day, Month, and Year into column Birthday without removing them
result <- unite(data, col = "Birthday", Day, Month, Year, sep='/', remove = FALSE)
 
# Print the new data frame after merging
print(result)

Result:


    Name        Birthday Day    Month Year
1   John    22/June/1990  22     June 1990
2  David    13/July/1995  13     July 1995
3 Donald   15/March/1998  15    March 1998
4  James 24/October/1977  24  October 1977
5 Evelyn 3/December/2001   3 December 2001
6   Lily   5/August/2004   5   August 2004
7  Daisy     21/May/1988  21      May 1988

Summary

In summary, the unite() function can merge two or more two columns of a data frame into a new column. The function is useful when you want to join the same characteristic features. You can also drop the input columns or not by using the Boolean value to the parameter named remove.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *