0 votes
in R Language by
How to Replace Missing Values(NA) in R: na.omit & na.rm summary

1 Answer

0 votes
by

We have three methods to deal with missing values:

  • Exclude all of the missing observations
  • Impute with the mean
  • Impute with the median

The following table summarizes how to remove all the missing observations

LibraryObjectiveCode
baseList missing observations
colnames(df)[apply(df, 2, anyNA)]
dplyrRemove all missing values
na.omit(df)

Imputation with mean or median can be done in two ways

  • Using apply
  • Using sapply

MethodDetailsAdvantagesDisadvantages
Step by step with applyCheck columns with missing, compute mean/median, store the value, replace with mutate()You know the value of means/medianMore execution time. Can be slow with big dataset
Quick way with sapplyUse sapply() and data.frame() to automatically search and replace missing values with mean/medianShort code and fastDon't know the imputation values

Related questions

+3 votes
asked Jul 28, 2019 in R Language by Aarav2017
0 votes
asked Nov 4, 2019 in R Language by MBarbieri
...