View source: R/remove_empties.R
remove_empty | R Documentation |
Removes all rows and/or columns from a data.frame or matrix that
are composed entirely of NA
values.
remove_empty(dat, which = c("rows", "cols"), cutoff = 1, quiet = TRUE)
dat |
the input data.frame or matrix. |
which |
one of "rows", "cols", or |
cutoff |
Under what fraction (>0 to <=1) of non-empty rows or columns should
|
quiet |
Should messages be suppressed ( |
Returns the object without its missing rows or columns.
remove_constant()
for removing
constant columns.
Other remove functions:
remove_constant()
# not run:
# dat %>% remove_empty("rows")
# addressing a common untidy-data scenario where we have a mixture of
# blank values in some (character) columns and NAs in others:
library(dplyr)
dd <- tibble(
x = c(LETTERS[1:5], NA, rep("", 2)),
y = c(1:5, rep(NA, 3))
)
# remove_empty() drops row 5 (all NA) but not 6 and 7 (blanks + NAs)
dd %>% remove_empty("rows")
# solution: preprocess to convert whitespace/empty strings to NA,
# _then_ remove empty (all-NA) rows
dd %>%
mutate(across(where(is.character), ~ na_if(trimws(.), ""))) %>%
remove_empty("rows")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.