R/remove_column.R

Defines functions remove_column

Documented in remove_column

#' @title Delete columns with all NA or all identical data.
#' @keywords internal
#'
#' @description Delete columns where all data elements are NA or the same.
#'
#' @param dt data.table, to manipulate.
#' @param na boolean, to delete columns where all data elements are NA.
#' @param identical boolean, to delete columns where all data elements are the same.
#'
#' @return data table, with data.
#'
#' @encoding UTF-8
#' @importFrom data.table :=

remove_column <- function(dt, na = TRUE, identical = TRUE) {
  if(na) {
    na_cols <- sapply(dt, function(x) all(is.na(x)))
    if(any(na_cols)) {dt[, (colnames(dt)[na_cols]):=NULL]}
  }
  if(identical) {
    identical_cols <- sapply(dt, function(x) length(unique(x)) == 1)
    if(any(identical_cols)) {dt[, (colnames(dt)[identical_cols]):=NULL]}
  }
  return(dt)
}

Try the parseRPDR package in your browser

Any scripts or data that you put into this service are public.

parseRPDR documentation built on March 31, 2023, 11:36 p.m.