drop_empty_cols: Drop 'empty' columns in a dataframe

View source: R/dataframe_tools.R

drop_empty_colsR Documentation

Drop 'empty' columns in a dataframe

Description

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.

Usage

drop_empty_cols(
  df,
  from = 1,
  to = NULL,
  cols = NULL,
  regex = "^$",
  report = FALSE
)

Arguments

df

(Dataframe) A dataframe.

from, to

(Numeric or NULL) The start and end of a continuous range of columns that will be subsetted from df. If to is NULL, it defaults to the last column in df so that ⁠from = 2, to = NULL⁠ is the same as 2:length(df).

cols

(Numeric or NULL) A numeric vector of the columns to consider. This allows you to select non-contiguous columns. If the cols argument is being used (not-NULL), from and to will be ignored.

regex

(Character) A regex pattern that matches a value that should be considered 'empty'.

report

(Logical) If TRUE, print a Message with the names of the empty columns that were dropped.

Value

A subset of df with all empty columns removed.

Authors

Examples

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


DesiQuintans/desiderata documentation built on April 9, 2023, 5:43 a.m.