You can remove rows with NAs in various ways with the drop_na()
function.
Remove rows that contain at least one NA value
Remove rows with NAs in one specific column
Remove rows with NAs in multiple specific columns
Tidyverse reference page
Dataset
For demonstration we’ll load the mammal_sleep_tbl
data from the mgrtibbles package (hyperlink includes install instructions).
#Load package
library ("mgrtibbles" )
#mammal_sleep_tbl tibble for demonstration
mgrtibbles:: mammal_sleep_tbl
# A tibble: 62 × 11
species body_wt brain_wt non_dreaming dreaming total_sleep life_span
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Africanelephant 6.65e+3 5.71 NA NA 3.3 38.6
2 Africangiantpou… 1 e+0 0.0066 6.3 2 8.3 4.5
3 ArcticFox 3.38e+0 0.0445 NA NA 12.5 14
4 Arcticgroundsqu… 9.2 e-1 0.0057 NA NA 16.5 NA
5 Asianelephant 2.55e+3 4.60 2.1 1.8 3.9 69
6 Baboon 1.06e+1 0.180 9.1 0.7 9.8 27
7 Bigbrownbat 2.3 e-2 0.0003 15.8 3.9 19.7 19
8 Braziliantapir 1.6 e+2 0.169 5.2 1 6.2 30.4
9 Cat 3.3 e+0 0.0256 10.9 3.6 14.5 28
10 Chimpanzee 5.22e+1 0.44 8.3 1.4 9.7 50
# ℹ 52 more rows
# ℹ 4 more variables: gestation <dbl>, predation <fct>, exposure <fct>,
# danger <fct>
Na in any column
Drop rows with an NA in any column with drop_na()
.
mammal_sleep_tbl |>
tidyr:: drop_na ()
# A tibble: 42 × 11
species body_wt brain_wt non_dreaming dreaming total_sleep life_span
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Africangiantpou… 1 e+0 0.0066 6.3 2 8.3 4.5
2 Asianelephant 2.55e+3 4.60 2.1 1.8 3.9 69
3 Baboon 1.06e+1 0.180 9.1 0.7 9.8 27
4 Bigbrownbat 2.3 e-2 0.0003 15.8 3.9 19.7 19
5 Braziliantapir 1.6 e+2 0.169 5.2 1 6.2 30.4
6 Cat 3.3 e+0 0.0256 10.9 3.6 14.5 28
7 Chimpanzee 5.22e+1 0.44 8.3 1.4 9.7 50
8 Chinchilla 4.25e-1 0.0064 11 1.5 12.5 7
9 Cow 4.65e+2 0.423 3.2 0.7 3.9 30
10 EasternAmerican… 7.5 e-2 0.0012 6.3 2.1 8.4 3.5
# ℹ 32 more rows
# ℹ 4 more variables: gestation <dbl>, predation <fct>, exposure <fct>,
# danger <fct>
NA in one column
Drop rows with an NA in one specific column.
mammal_sleep_tbl |>
tidyr:: drop_na (life_span)
# A tibble: 58 × 11
species body_wt brain_wt non_dreaming dreaming total_sleep life_span
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Africanelephant 6.65e+3 5.71 NA NA 3.3 38.6
2 Africangiantpou… 1 e+0 0.0066 6.3 2 8.3 4.5
3 ArcticFox 3.38e+0 0.0445 NA NA 12.5 14
4 Asianelephant 2.55e+3 4.60 2.1 1.8 3.9 69
5 Baboon 1.06e+1 0.180 9.1 0.7 9.8 27
6 Bigbrownbat 2.3 e-2 0.0003 15.8 3.9 19.7 19
7 Braziliantapir 1.6 e+2 0.169 5.2 1 6.2 30.4
8 Cat 3.3 e+0 0.0256 10.9 3.6 14.5 28
9 Chimpanzee 5.22e+1 0.44 8.3 1.4 9.7 50
10 Chinchilla 4.25e-1 0.0064 11 1.5 12.5 7
# ℹ 48 more rows
# ℹ 4 more variables: gestation <dbl>, predation <fct>, exposure <fct>,
# danger <fct>
NA in multiple specific columns
Drop rows with an NA in at least one of the specified columns.
Below a character vector of column names is used c("non_dreaming","dreaming")
. The following range of column names would also work in this situation (non_dreaming:dreaming
).
Note: There is an NA in the 10th row of the life_span column as we did not specify it with drop_na()
.
mammal_sleep_tbl |>
tidyr:: drop_na (c ("non_dreaming" ,"dreaming" ))
# A tibble: 48 × 11
species body_wt brain_wt non_dreaming dreaming total_sleep life_span
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Africangiantpou… 1 e+0 0.0066 6.3 2 8.3 4.5
2 Asianelephant 2.55e+3 4.60 2.1 1.8 3.9 69
3 Baboon 1.06e+1 0.180 9.1 0.7 9.8 27
4 Bigbrownbat 2.3 e-2 0.0003 15.8 3.9 19.7 19
5 Braziliantapir 1.6 e+2 0.169 5.2 1 6.2 30.4
6 Cat 3.3 e+0 0.0256 10.9 3.6 14.5 28
7 Chimpanzee 5.22e+1 0.44 8.3 1.4 9.7 50
8 Chinchilla 4.25e-1 0.0064 11 1.5 12.5 7
9 Cow 4.65e+2 0.423 3.2 0.7 3.9 30
10 Deserthedgehog 5.5 e-1 0.0024 7.6 2.7 10.3 NA
# ℹ 38 more rows
# ℹ 4 more variables: gestation <dbl>, predation <fct>, exposure <fct>,
# danger <fct>