remove_ifall_cols: Remove all columns that match one or more critieria

View source: R/remove_ifall_cols.R

remove_ifall_colsR Documentation

Remove all columns that match one or more critieria

Description

For a data.frame, drop all columns that satisfy a condition or set of conditions

Usage

remove_ifall_cols(
  df,
  ifallwhat = NULL,
  factor_as_chr = TRUE,
  drop_all_NA = TRUE
)

Arguments

df

A data.frame (or data.table)

ifallwhat

A vector of value(s) to test for. NA, if included, will be dropped. If not specified, defaults to a blank zero-length character, i.e. "".

factor_as_chr

If ifallwhat is of class character, should columns of class factor be treated as character (and therefore subject to deletion as well)?

drop_all_NA

Should columns that are all NA be dropped? Defaults to TRUE

Value

A data.frame, or a data.table if this was passed to df, with all columns that match the requirements within ifallwhat removed. If no columns in df met the criteria requested, then the input is returned without modification.

Note

This function will coerce the input data.frame into a data.table via as.data.table as an intermediate step. If a data.frame is detected as input, setDF is used for output coercion.

Examples

my_df <- data.frame(
A = 1:3L,
B = rep(numeric(3), 3),
C = rep("", 3),
D = rep(NA_real_, 3),
stringsAsFactors = FALSE
)

my_df_1 <- remove_ifall_cols(my_df, numeric(3))
my_df_2 <- remove_ifall_cols(my_df, numeric(3), drop_all_NA = FALSE)
remove_ifall_cols(my_df, c(""), drop_all_NA = FALSE)

remove_ifall_cols(my_df, c(1:3))
remove_ifall_cols(my_df, c(1:3), drop_all_NA = FALSE)
remove_ifall_cols(my_df, as.numeric(1:3), drop_all_NA = FALSE) # does nothing

slin30/wzMisc documentation built on Jan. 27, 2023, 1 a.m.