coalesce R Function of dplyr Package (2 Examples)

 

In this article you’ll learn how to apply the coalesce function of the dplyr add-on package in R programming.

The table of content looks as follows:

If you want to know more about these contents, keep reading!

 

Example 1: Use coalesce Function to Replace Missing Values with One Value

Example 1 explains how to replace missing values by one specific value in R. First, we need to install and load the dplyr package of the tydiverse environment:

install.packages("dplyr")        # Install and load dplyr package
library("dplyr")

Furthermore, we need to create some example data:

x <- c(2, 1, NA, 5, 3, NA)       # Create example vector

Our example data is a numeric vector with two NA values.

Now, we can use the coalesce function to replace these NA values by a different value. In this example, we are replacing the NA values with the number 999:

coalesce(x, 999)                 # Apply coalesce function
# 2   1 999   5   3 999

As you can see based on the output of the RStudio console, our updated vector contains the value 999 at each NA position.

 

Example 2: Match Two Vectors with coalesce Function

We can also use the coalesce command to merge two numeric vectors into one vector. Let’s first create a second vector:

y <- c(1, NA, 7, NA, 7, NA)      # Create second example vector

Note that our second vector also contains missing values.

Now, we can match our two vectors x and y as follows:

coalesce(x, y)                   # Match two vectors
# 2  1  7  5  3 NA

As you can see, the coalesce command replaced each missing value of x with the corresponding value in y. Note that the last vector element is still NA, since both input vectors contain an NA value at this position.

 

Video, Further Resources & Summary

Do you need more info on the dplyr package? Then you could watch the following video of my YouTube channel. In the video, I’m explaining other functions of the dplyr package in RStudio:

 

 

Additionally, I can recommend to read the other articles of this website.

 

Summary: In this R programming tutorial you learned how to replace NA values with the coalesce dplyr function. If you have further questions, don’t hesitate to let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

  • Susanne Ebert
    July 18, 2022 2:23 pm

    Hi Joachim, I have a question concerning the coalesce function. Is it only possible to use it with vectors or is it also possible to use it for variables in a dataframe? For example: I have a data frame like this

    testdata <- data.frame(
    ID = 1:6,
    Item1 = c(1,1,1,NA,NA,NA),
    Item2 = c(NA,NA,NA,2,2,2),
    Item3 = c(1:6)
    )

    and I want to built a new variable Item4 out of Item1 and Item2 so that Item4 = c(1,1,1, 2,2,2). Can you help me?

    Thanks, Susanne

    Reply
    • Hi Susanne,

      Yes, this is possible. Please have a look at the following example code:

      testdata <- data.frame(
        ID = 1:6,
        Item1 = c(1,1,1,NA,NA,NA),
        Item2 = c(NA,NA,NA,2,2,2),
        Item3 = c(1:6)
      )
       
      testdata$Item4 <- coalesce(testdata$Item1,
                                 testdata$Item2)
      testdata
      #   ID Item1 Item2 Item3 Item4
      # 1  1     1    NA     1     1
      # 2  2     1    NA     2     1
      # 3  3     1    NA     3     1
      # 4  4    NA     2     4     2
      # 5  5    NA     2     5     2
      # 6  6    NA     2     6     2

      Regards,
      Joachim

      Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top