18.1 C
London
Wednesday, August 13, 2025
HomeStatistics TutorialRHow to Replace NAs with Strings in R (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories