# Replace the first non-missing value in R

Replace the first non-missing value in R, to retrieve the first non-missing value in each place of one or more vectors, use the coalesce() function from the dplyr package in R.

There are two typical applications for this function:

## Example 1: To replace missing values in a vector, use coalesce()

The coalesce() function can be used to fill in all missing values in a vector with a value of 100 using the following code.

How to combine Multiple Plots in R » finnstats

library(dplyr)

Let’s create a vector of values

x <- c(15, NA, 123, NA, 25, 104, 29)

Now we can replace missing values with 999

coalesce(x, 999) [1] 15 999 123 999 25 104 29

If you look at the original vector, you’ll see that every NA value has been changed to a value of 100.

## Example 2: To return the first non-missing value across all data frame columns, use coalesce().

Let’s say we have the R data frame shown below:

Let’s create a data frame

df <- data.frame(A=c(110, NA, 25, 36, NA, 227, NA), B=c(104, 95, NA, 13, NA, 120, 44))

Now we can view the data frame

df

A B 1 110 104 2 NA 95 3 25 NA 4 36 13 5 NA NA 6 227 120 7 NA 44

The first non-missing value across columns A and B in the data frame can be returned by using the coalesce() function, as demonstrated by the code below.

PCA for Categorical Variables in R » finnstats

library(dplyr)

make a new column from the values of columns A and B.

df$C <- coalesce(df$A, df$B)

Now we can view the updated data frame

df

A B C 1 110 104 110 2 NA 95 95 3 25 NA 25 4 36 13 36 5 NA NA NA 6 227 120 227 7 NA 44 44

The first value in columns A and B that is not missing is found in column C as a result.

Observe that row 5, whose columns A and B both had NA values, has a value of NA in column C.

If there are NA values in any of the columns, we can simply add one more value to the coalesce() function to be used as the value.

Missing Value Imputation in R » finnstats