How to Remove NA Values from Vectors in R
When you remove NA values from vectors and data frames, you strip out missing observations that would otherwise bias summary statistics, model fitting, and plotting. Use na.omit() in base R for a quick row‑wise drop, or drop_na() from tidyr when you need column‑specific control inside a tidyverse pipeline.
# Remove NA from a vector
x <- c(1, 2, NA, 4, NA, 6)
x_clean <- na.omit(x)
# Remove rows with any NA from a data frame
df_clean <- na.omit(df)
# dplyr/tidyr: drop NA from specific columns only
library(dplyr)
library(tidyr)
df_clean <- df %>% drop_na(column_name)
The base R approach x[!is.na(x)] is terse but less readable in pipelines than na.omit(). For data frames, complete.cases() returns a logical vector you can use for manual subsetting. drop_na() accepts multiple column names: drop_na(df, col1, col2) drops rows only when those columns are NA. Before removing rows, consider whether the missingness pattern is informative — naniar::gg_miss_var() visualizes missingness patterns, and imputation is often a better choice than deletion. To check how many observations you would lose before committing to removal, sum(!complete.cases(df)) gives the affected row count. When filtering by NA in a single column, filter(df, !is.na(col)) drops only those rows while preserving observations with missingness elsewhere.