• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rstats 101

Learn R Programming Tips & Tricks for Statistics and Data Science

  • Home
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to replace NA in a column with specific value

rstats101 · June 17, 2022 ·

In this tutorial we will learn how to replace missing values/NA in a column with a specific value. We will replace NA in a column using two approaches. At first we will use dplyr function mutate and ifelse to identify the element(s) with NA and replace with a specific value. Next we will use base R approach to replace missing value(s) in a column.

To get started let us load the packages needed.

library(tidyverse)

And also create a simple dataframe from scratch. Here create tibble, tidyverse variant of data frame using tribble() function.

sales <- tibble::tribble(
  ~quarter, ~year, ~sales,
  "Q1",    2000,    6603,
  "Q2",      2000,    7182,
  "Q3",      2000,    8175,
  NA,      2000,  9001)

The dataframe has three columns and the first column has one missing value, NA, in the last row.

sales

## # A tibble: 4 × 3
##   quarter  year sales
##   <chr>   <dbl> <dbl>
## 1 Q1       2000  6603
## 2 Q2       2000  7182
## 3 Q3       2000  8175
## 4 <NA>     2000  9001

Replace NA in column with a specific value using tidyverse

Let us say we want to replace the missing value with a specific value “Q4”, we can use mutate() function to update the column with a new one. We use ifelse() function identify missing value element and replace it with the value we want.

sales %>%
  mutate(quarter=ifelse(is.na(quarter),"Q4",quarter))
## # A tibble: 4 × 3
##   quarter  year sales
##   <chr>   <dbl> <dbl>
## 1 Q1       2000  6603
## 2 Q2       2000  7182
## 3 Q3       2000  8175
## 4 Q4       2000  9001

Replace NA in column with a specific value using base R

If were to use base R function to replace missing value in a column, we will first identify the index where there is NA in the column using is.na() function and assign the value of interest as shown below.

sales$quarter[is.na(sales$quarter)] <- "Q4"

sales
## # A tibble: 4 × 3
##   quarter  year sales
##   <chr>   <dbl> <dbl>
## 1 Q1       2000  6603
## 2 Q2       2000  7182
## 3 Q3       2000  8175
## 4 Q4       2000  9001

Related

Filed Under: dplyr, replace NA, replace NAs tidyverse Tagged With: replace missing value in a column, replace NA in a column with specific value

Primary Sidebar

Recent Posts

  • How to create a nested dataframe with lists
  • How to compute proportion with tidyverse
  • How to Compute Z-Score of Multiple Columns
  • How to drop unused level of factor variable in R
  • How to compute Z-score

Categories

%in% arrange() as.data.frame as_tibble built-in data R colSums() R cor() in R data.frame dplyr dplyr across() dplyr group_by() dplyr rename() dplyr rowwise() dplyr row_number() dplyr select() dplyr slice_max() dplyr slice_sample() drop_na R duplicated() gsub head() impute with mean values is.element() linear regression matrix() function na.omit R NAs in R near() R openxlsx pivot_longer() prod() R.version replace NA replace NAs tidyverse R Function rstats rstats101 R version scale() sessionInfo() t.test() tidyr tidyselect tidyverse write.xlsx

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version