Built with Tutify.io

    AIStudyLabs

    Explain unite() Function with Examples

    Explain unite() Function with Examples

    The unite() function of the tidyr package in R is a powerful tool for data manipulation and cleaning. It combines multiple data frame columns into a single column based on a specified separator. This function is particularly useful when merging information from multiple columns for reporting, data transformation, or analysis. In contrast, the separate() function, also part of the tidyr package, performs the opposite task by splitting a single column into multiple columns, making it another essential transformation function for reshaping data.

    In this article, I will explain an overview of the unite() function, including its syntax, parameters, and usage, to demonstrate how to combine multiple columns into a single column in a data frame.

    <a id="h-r-unite-function"></a>

    R unite() Function

    The unite() function in R reshapes the data frame by merging two or more columns into one. It provides flexibility with parameters like sep for custom separators, remove for retaining or removing original columns, and for automatic handling of NA values.

    Syntax of the unite() Function

    Below is the syntax of the unite() function.

    
    # Syntax of the unite()
    unite(data, col, ..., sep = "_", remove = TRUE)
    

    <a id="h-parameters"></a>

    Parameters

    <a id="h-return-value"></a>

    Return Value

    This function returns a modified data frame with the specified columns united into a single column.

    <a id="h-unite-multiple-columns-into-one-r-column"></a>

    Unite Multiple Columns into One R Column

    To merge multiple columns into a single column using the unite() function, first load the tidyr package. Specify the columns to combine by listing their names directly or providing a character vector in the ... argument of the unite() function. By default, an underscore (_) uses as the delimiter for merging the data.

    
    # Combine columns into one using unite()
    # Load the tidyr library
    library(tidyr)
    
    df <- data.frame(
      Student = c("Geetha", "Ram", "Sai"),
      History = c(89, 81, 78),
      Math = c(75, 88, 85),
      Science = c(85, 92, 90),
      Total = c(249, 261, 253),
      Percentage = c("83%", "87%", "84%")
    )
    print("Original Data frame:")
    print(df)
    
    # Unite columns
    united_data <- unite(
      df,
      col = "Total_percentage",
      Total, Percentage
      )
    print("Data After Uniting Columns:")
    print(united_data)
    

    Yields below output.

    <a id="h-customize-separator-with-sep"></a>

    Customize Separator with sep

    You can customize the delimiter between the combined column with the sep parameter. By default, the function combines columns using an underscore (_) as the separator.

    
    # Customize Separator in unite() function
    united_data <- unite(
      df,
      col = "Total_percentage",
      Total, Percentage,
      sep = ","
      )
    print("Data After Uniting Columns:")
    print(united_data)
    

    Yields below output.

    <a id="h-retain-original-columns-with-remove-param"></a>

    Retain Original Columns with remove Param

    By default, the unite() function removes the original columns after uniting them. You can use remove = FALSE to retain the original columns along with the new column.

    
    # Retain original column
    united_data <- unite(
      df,
      col = "Total_percentage",
      Total, Percentage,
      sep = ",",
      remove = FALSE
      )
    print("Data After Uniting Columns:")
    print(united_data)
    

    Yields below output.

    
    # Output:
    [1] "Data After Uniting Columns:"
    
    > print(united_data)
      Student History Math Science Total_percentage Total Percentage
    1  Geetha      89   75      85          249,83%   249        83%
    2     Ram      81   88      92          261,87%   261        87%
    3     Sai      78   85      90          253,84%   253        84%
    

    Remove Missing Values with na.rm

    You can handle missing values in a combined column by setting na.rm = TRUE, which removes them from the resulting column.

    
    # Remove Missing Values with na.rm
    # Load the tidyr library
    library(tidyr)
    
    df <- data.frame(
      Student = c("Geetha", "Ram", "Sai"),
      History = c(89, 81, 78),
      Math = c(75, NA, 85),
      Science = c(85, NA, 90),
      Total = c(NA, 261, 253),
      Percentage = c("83%", "87%", "84%")
    )
    print("Original Data frame:")
    print(df)
    
    # Unite columns
    united_data <- unite(
      df,
      col = "Total_percentage",
      Total, Percentage,
      sep = ",",
      na.rm = TRUE
      )
    print("After removing NA's from Column:")
    print(united_data)
    

    Yields below output.

    
    # Output:
    [1] "After removing NA's from Column:"
      Student History Math Science Total_percentage
    1  Geetha      89   75      85              83%
    2     Ram      81   NA      NA          261,87%
    3     Sai      78   85      90          253,84%
    

    <a id="h-frequently-asked-questions-of-unite"></a>

    Frequently Asked Questions of unite()

    The unite() function combines multiple columns in a data frame into a single column using a specified separator, simplifying data organization and preparation

    The unite() function is part of the tidyr package. You need to load the tidyr library using library(tidyr) before using the function.

    You can specify the columns to combine by listing their names directly or using a character vector in the ... argument of the unite() function.

    By default, the original columns are removed after combining. You can retain them by setting remove = FALSE.

    The na.rm parameter controls how missing values are handled. By setting na.rm = TRUE, any NA values are excluded from the combined column.

    While paste() and paste0() are base R functions used for concatenating vectors, unite() is specifically designed for data frames and provides additional functionality like automatic removal of original columns and handling of NA values.

    Conclusion

    In this article, I have explained the unite() function in R’s tidyr package is a flexible and efficient tool for combining multiple columns into one. By customizing parameters such as sep, remove, and na.rm, you can tailor the function to suit your specific data manipulation needs. Whether you’re preparing data for visualization or simplifying a dataset, unite() helps streamline the process.

    Happly Learning!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References

    https://www.rdocumentation.org/packages/tidyr/versions/0.8.3/topics/unite