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

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

by Erma Khan

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 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           92
4        Master     90

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

#view updated data frame
df 

   status education income
1  single     Assoc     34
2 married      Bach     88
3 married           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 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           92
4        Master     90

#replace missing values with 'single' in status column
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

Additional Resources

How to Remove Rows with Some or All NAs in R
How to Replace NA with Zero in dplyr

Related Posts