View source: R/dataframe_tools.R
drop_empty_cols | R Documentation |
Deletes columns from a dataframe if they are 'empty'. A column is empty when every
single row is NA
, NULL
, ""
, or matches a regular expression.
drop_empty_cols(
df,
from = 1,
to = NULL,
cols = NULL,
regex = "^$",
report = FALSE
)
df |
(Dataframe) A dataframe. |
from, to |
(Numeric or |
cols |
(Numeric or |
regex |
(Character) A regex pattern that matches a value that should be considered 'empty'. |
report |
(Logical) If |
A subset of df
with all empty columns removed.
Desi Quintans (http://www.desiquintans.com)
data <- data.frame(a = c(1, 2, 3),
b = c(0, 0, 0),
c = c(1, 1, 0),
d = c("", "", ""),
e = c("moo", "baa", "woof"))
#> a b c d e
#> 1 0 1 moo
#> 2 0 1 baa
#> 3 0 0 woof
drop_empty_cols(data)
#> a b c e
#> 1 0 1 moo
#> 2 0 1 baa
#> 3 0 0 woof
drop_empty_cols(data, regex = "moo|baa|woof")
#> a b c
#> 1 0 1
#> 2 0 1
#> 3 0 0
drop_empty_cols(data, regex = "moo|baa|woof", report = TRUE)
#> Empty cols dropped: d, e
#> a b c
#> 1 0 1
#> 2 0 1
#> 3 0 0
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.