Replace missing values with dplyr
Replace missing values with dplyr, you’ll discover how to use the coalesce function of the dplyr add-on package in R programming in this article.
Example 1: Use the coalesce function to add one value in place of any missing values.
How to replace missing values with dplyr with a single specified value is demonstrated in Example 1.
Installing and loading the tydiverse environment’s dplyr package is the first step.
Additionally, we must generate some sample data:
x <- c(32, 11, NA, 5, 3, NA) x  2 1 NA 5 3 NA
A numeric vector with two NA values makes up our example data.
Now that these NA values have been replaced by a different value, we may use the coalesce method. In this instance, we are substituting the value 999 for the NA values:
coalesce(x, 999)  32 11 999 5 3 999
As you can see from the RStudio console’s output, the value is there in our modified vector.
Example 2: Use the coalesce function to match two vectors.
The coalesce command can also be used to combine two numerical vectors into a single vector. Let’s begin by producing a second vector:
y <- c(1, NA, 17, NA, 37, NA) y  1 NA 17 NA 37 NA
Also, take note that our second vector has missing data.
Our two vectors, x and y, can now be matched as follows:
coalesce(x, y)  2 1 17 5 3 NA
As you can see, the coalesce command substituted the equivalent value in y for each missing value in x. Due to the NA value present in both of the input vectors at this location, the final vector element is still NA.