It is common to have NAs in your data. However, sometimes instead of removing them you may want to replace them. below are three methods to carry this out.
Replace all NAs in a column with a specific value with tidyr::replace_na(). A common example is to replace all NAs with 0.
Use the corresponding value of another column to replace NAs.
For demonstration we’ll load the mammal_sleep_tbl data from the mgrtibbles package (hyperlink includes install instructions). Additionally, we’ll subset it to:
Keep the first ten rows with at least one NA.
COlumns species to life_span
#Load packagelibrary("mgrtibbles")#mammal_sleep_tbl tibble for demonstration#Subset to only keep rows with at least one NAmammal_sleep_na_tbl <- mgrtibbles::mammal_sleep_tbl[!complete.cases(mammal_sleep_tbl),] |>#Slice to keep first ten rows and select columns species to life_span dplyr::slice(1:10) |> dplyr::select(species:life_span)#View tibblemammal_sleep_na_tbl
# A tibble: 10 × 7
species body_wt brain_wt non_dreaming dreaming total_sleep life_span
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Africanelephant 6654 5.71 NA NA 3.3 38.6
2 ArcticFox 3.38 0.0445 NA NA 12.5 14
3 Arcticgroundsqu… 0.92 0.0057 NA NA 16.5 NA
4 Deserthedgehog 0.55 0.0024 7.6 2.7 10.3 NA
5 Donkey 187. 0.419 NA NA 3.1 40
6 Genet 1.41 0.0175 4.8 1.3 6.1 34
7 Giantarmadillo 60 0.081 12 6.1 18.1 7
8 Giraffe 529 0.68 NA 0.3 NA 28
9 Gorilla 207 0.406 NA NA 12 39.3
10 Graywolf 36.3 0.120 NA NA 13 16.2
Replace with a value
Replace NAs in specified columns with tidyr::replace_na().
When used with a tibble/data.frame the function needs to be provided with a list. The list contains the column names and the replacement value.