The “coalesce” Function in R

  • Package: dplyr

  • Purpose: To return the first non-missing value from a set of vectors.

  • General Class: Data Manipulation

  • Required Argument(s):

    • ...: Vectors or column names to evaluate.

  • Notable Optional Arguments:

    • None.

  • Example (with Explanation):

  • # Load necessary packages
    library(dplyr)

    # Create a sample data frame
    data <- data.frame(
    ID = c(1, 2, 3, 4, 5),
    value1 = c(NA, 15, NA, 25, NA),
    value2 = c(10, NA, 20, NA, 30)
    )

    # Create a new column 'filled_value' with the first non-missing value from 'value1' and 'value2'
    result <- data %>%
    mutate(filled_value = coalesce(value1, value2))

    # Display the result
    print(result)

  • In this example, the coalesce function from the dplyr package is used to create a new column ‘filled_value’ in the sample data frame data. The ‘filled_value’ column is assigned the first non-missing value from the ‘value1’ and ‘value2’ columns for each row. If both ‘value1’ and ‘value2’ are missing for a row, the ‘filled_value’ will also be missing. The result is a new data frame result containing the original data along with the newly created ‘filled_value’ column.

Previous
Previous

The “na_if” Function in R

Next
Next

The “if_else” Function in R