How to Replace NA Values with Specific Values in R
To replace NA values with a specific value in R, the simplest approach is base R subsetting, while coalesce() and replace_na() offer cleaner tidyverse alternatives. Each method handles single columns, multiple columns, or entire data frames. Choosing the right approach depends on whether you are working interactively, inside a pipeline, or on data too large to copy.
# Base R: replace NA in a vector
x <- c(1, 2, NA, 4, NA, 6)
x[is.na(x)] <- 0
# dplyr: coalesce() returns first non-NA value
library(dplyr)
x <- coalesce(x, 0)
# data.table: modify by reference
library(data.table)
dt <- data.table(df)
dt[is.na(score), score := 0]
For data frames, df %>% mutate(score = coalesce(score, 0)) replaces NA in a specific column. To replace NA across all columns at once: df %>% mutate(across(everything(), ~coalesce(.x, 0))). replace_na() from tidyr takes a named list: df %>% replace_na(list(score = 0)). When imputing with column statistics, df %>% mutate(value = replace_na(value, mean(value, na.rm = TRUE))) computes the replacement once for the whole column. Base R’s df$score[is.na(df$score)] <- 0 modifies in place and works without dependencies. coalesce() is the most flexible option — it accepts multiple inputs and returns the first non‑NA value, useful for fallback chaining across columns. For instance, coalesce(col_a, col_b, 0) tries col_a first, then col_b, then defaults to zero.