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:
- Example 1: Use coalesce Function to Replace Missing Values with One Value
- Example 2: Match Two Vectors with coalesce Function
- Video, Further Resources & Summary
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:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Additionally, I can recommend to read the other articles of this website.
- Replace NA with Last Observed Value
- Replace NA with 0
- What are
Values? - is.na Function
- How to Remove NA Values From Vector
- Introduction to dplyr
- R Functions List (+ Examples)
- The R Programming Language
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.
Statistics Globe Newsletter
2 Comments. Leave new
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
Hi Susanne,
Yes, this is possible. Please have a look at the following example code:
Regards,
Joachim