R/select_numeric_cols.R

Defines functions select_numeric_cols

Documented in select_numeric_cols

#' @title Select Numeric Columns
#'
#' @description
#' Returns a tibble with only the numeric columns of the input, and optionally drops rows with NAs.
#'
#' @param dataset A vector, matrix, data frame, or tibble.
#' @param remove_na Logical. If TRUE, rows with any NA values will be dropped. Default is FALSE.
#'
#' @return A tibble with only numeric columns.
#'
#' @examples
#' select_numeric_cols(iris)
#' @rdname select_numeric_cols
#' @export
select_numeric_cols <- function(dataset, remove_na = FALSE) {
  dataset <- tibble::as_tibble(dataset)
  
  result <- dataset %>%
    dplyr::select(dplyr::where(is.numeric))
  
  if (remove_na) {
    result <- tidyr::drop_na(result)
  }
  
  return(result)
}

Try the tidySummaries package in your browser

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

tidySummaries documentation built on June 8, 2025, 11:35 a.m.