How to Replace NAs with Strings in R (With Examples)

Replacing NAs (missing values) with strings in R can be done using the is.na() and ifelse() functions. The is.na() function returns a logical vector with TRUE for missing values and FALSE for present values. The ifelse() function is used to create a vector which returns a value when the condition is TRUE and another value when the condition is FALSE. An example of this would be ifelse(is.na(x), “missing”, x) which returns “missing” when x is missing and x when x is present.


You can use the replace_na() function from the tidyr package to replace NAs with specific strings in  a column of a data frame in R:

#replace NA values in column x with "missing"
df$x %>% replace_na('none')

You can also use this function to replace NAs with specific strings in multiple columns of a data frame:

#replace NA values in column x with "missing" and NA values in column y with "none"
df %>% replace_na(list(x = 'missing', y = 'none')) 

The following examples show how to use this function in practice.

Example 1: Replace NAs with Strings in One Column

The following code shows how to replace NAs with a specific string in one column of a data frame:

library(tidyr)

df <- data.frame(status=c('single', 'married', 'married', NA),
                 education=c('Assoc', 'Bach', NA, 'Master'),
                 income=c(34, 88, 92, 90))

#view data frame
df

   status education income
1  single     Assoc     34
2 married      Bach     88
3 married      <NA>     92
4    <NA>    Master     90

#replace missing values with 'single' in status column
df$status <- df$status %>% replace_na('single')

#view updated data frame
df 

   status education income
1  single     Assoc     34
2 married      Bach     88
3 married      <NA>     92
4  single    Master     90

Example 2: Replace NAs with Strings in Multiple Columns

The following code shows how to replace NAs with a specific string in multiple columns of a data frame:

library(tidyr)

df <- data.frame(status=c('single', 'married', 'married', NA),
                 education=c('Assoc', 'Bach', NA, 'Master'),
                 income=c(34, 88, 92, 90))

#view data frame
df

   status education income
1  single     Assoc     34
2 married      Bach     88
3 married      <NA>     92
4    <NA>    Master     90

#replace missing values with 'single' in status column
df <- df %>% replace_na(list(status = 'single', education = 'none'))

#view updated data frame
df 

   status education income
1  single     Assoc     34
2 married      Bach     88
3 married      none     92
4  single    Master     90

x