tidyr::drop_na()
get_help()
docs
The drop_na()
function is part of the {tidyr}
package, which is part of the {tidyverse}
.
It is used to remove rows containing NA
s from a tibble (data frame), either from the full dataset or from certain specified column(s).
To use this function, you need to either first load the {tidyr}
library, or always use the function with tidyr::drop_na()
notation.
# Load the library
library(tidyr)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::drop_na() tidyr
# Remove all NAs from the tibble
%>%
tibble drop_na()
%>%
tibble drop_na(column to drop NAs from)
%>%
tibble drop_na(columns, to, drop, NAs, from)
The examples below use the msleep
dataset. Learn more about this dataset with get_help("msleep")
.
# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Owl … Aotus omni Prim… <NA> 17 1.8 NA 7 0.0155 0.48
## 2 Moun… Aplo… herbi Rode… nt 14.4 2.4 NA 9.6 NA 1.35
## 3 Grea… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 0.00029 0.019
## 4 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 0.423 600
## 5 Thre… Brad… herbi Pilo… <NA> 14.4 2.2 0.767 9.6 NA 3.85
## 6 Nort… Call… carni Carn… vu 8.7 1.4 0.383 15.3 NA 20.5
# Remove all NAs in the dataset
%>%
msleep drop_na()
## # A tibble: 20 × 11
## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Grea… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 0.00029 0.019
## 2 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 0.423 600
## 3 Dog Canis carni Carn… domesticated 10.1 2.9 0.333 13.9 0.07 14
## 4 Guin… Cavis herbi Rode… domesticated 9.4 0.8 0.217 14.6 0.0055 0.728
## 5 Chin… Chin… herbi Rode… domesticated 12.5 1.5 0.117 11.5 0.0064 0.42
## 6 Less… Cryp… omni Sori… lc 9.1 1.4 0.15 14.9 0.00014 0.005
## 7 Long… Dasy… carni Cing… lc 17.4 3.1 0.383 6.6 0.0108 3.5
## 8 Nort… Dide… omni Dide… lc 18 4.9 0.333 6 0.0063 1.7
## 9 Big … Epte… inse… Chir… lc 19.7 3.9 0.117 4.3 0.0003 0.023
## 10 Horse Equus herbi Peri… domesticated 2.9 0.6 1 21.1 0.655 521
## 11 Euro… Erin… omni Erin… lc 10.1 3.5 0.283 13.9 0.0035 0.77
## 12 Dome… Felis carni Carn… domesticated 12.5 3.2 0.417 11.5 0.0256 3.3
## 13 Gold… Meso… herbi Rode… en 14.3 3.1 0.2 9.7 0.001 0.12
## 14 Hous… Mus herbi Rode… nt 12.5 1.4 0.183 11.5 0.0004 0.022
## 15 Rabb… Oryc… herbi Lago… domesticated 8.4 0.9 0.417 15.6 0.0121 2.5
## 16 Labo… Ratt… herbi Rode… lc 13 2.4 0.183 11 0.0019 0.32
## 17 East… Scal… inse… Sori… lc 8.4 2.1 0.167 15.6 0.0012 0.075
## 18 Thir… Sper… herbi Rode… lc 13.8 3.4 0.217 10.2 0.004 0.101
## 19 Pig Sus omni Arti… domesticated 9.1 2.4 0.5 14.9 0.18 86.2
## 20 Braz… Tapi… herbi Peri… vu 4.4 1 0.9 19.6 0.169 208.
# Remove all rows with NA in the `vore` column
%>%
msleep drop_na(vore)
## # A tibble: 56 × 11
## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake brainwt
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Owl monkey Aotus omni Prim… <NA> 17 1.8 NA 7 0.0155
## 2 Mountain be… Aplo… herbi Rode… nt 14.4 2.4 NA 9.6 NA
## 3 Greater sho… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 0.00029
## 4 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 0.423
## 5 Three-toed … Brad… herbi Pilo… <NA> 14.4 2.2 0.767 9.6 NA
## 6 Northern fu… Call… carni Carn… vu 8.7 1.4 0.383 15.3 NA
## 7 Dog Canis carni Carn… domesticated 10.1 2.9 0.333 13.9 0.07
## 8 Goat Capri herbi Arti… lc 5.3 0.6 NA 18.7 0.115
## 9 Guinea pig Cavis herbi Rode… domesticated 9.4 0.8 0.217 14.6 0.0055
## 10 Grivet Cerc… omni Prim… lc 10 0.7 NA 14 NA
## # … with 46 more rows, and 1 more variable: bodywt <dbl>
# Remove all rows with NA in the `vore` or `conservation` columns
%>%
msleep drop_na(vore, conservation)
## # A tibble: 40 × 11
## name genus vore order conservation sleep_total sleep_rem sleep_cycle awake brainwt
## <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mountain be… Aplo… herbi Rode… nt 14.4 2.4 NA 9.6 NA
## 2 Greater sho… Blar… omni Sori… lc 14.9 2.3 0.133 9.1 0.00029
## 3 Cow Bos herbi Arti… domesticated 4 0.7 0.667 20 0.423
## 4 Northern fu… Call… carni Carn… vu 8.7 1.4 0.383 15.3 NA
## 5 Dog Canis carni Carn… domesticated 10.1 2.9 0.333 13.9 0.07
## 6 Goat Capri herbi Arti… lc 5.3 0.6 NA 18.7 0.115
## 7 Guinea pig Cavis herbi Rode… domesticated 9.4 0.8 0.217 14.6 0.0055
## 8 Grivet Cerc… omni Prim… lc 10 0.7 NA 14 NA
## 9 Chinchilla Chin… herbi Rode… domesticated 12.5 1.5 0.117 11.5 0.0064
## 10 Star-nosed … Cond… omni Sori… lc 10.3 2.2 NA 13.7 0.001
## # … with 30 more rows, and 1 more variable: bodywt <dbl>