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


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

Additional Resources

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

Leave a Reply

Your email address will not be published.