The “unite” Function in R

  • Package: tidyr

  • Purpose: To unite multiple columns into a single column by concatenating their values.

  • General Class: Data Reshaping

  • Required Argument(s):

    • data: The data frame to reshape.

    • col: The name of the new column to create.

    • ...: Names of columns to unite.

  • Notable Optional Arguments:

    • sep: The separator to use between values. The default is “_“.

  • Example (with Explanation):

  • # Load necessary packages
    library(tidyr)

    # Create a sample data frame
    data <- data.frame(
    ID = 1:3,
    First = c("John", "Jane", "Bob"),
    Last = c("Doe", "Smith", "Johnson")
    )

    # Unite the 'First' and 'Last' columns into a single column 'Name'
    result <- unite(data, col = "Name", First, Last, sep = "_")

    # Display the result
    print(result)

  • In this example, the unite function from the tidyr package is used to reshape the sample data frame data by uniting the ‘First’ and ‘Last’ columns into a single column ‘Name’. The values from the ‘First’ and ‘Last’ columns are concatenated with _ as the separator. The result is a new data frame result containing the reshaped data with the specified columns united into a single column.

Previous
Previous

The “complete” Function in R

Next
Next

The “separate” Function in R