R/count_row_non_missing.R

Defines functions count_row_non_missing

Documented in count_row_non_missing

#' count number of non-missing entries
#'
#' @param df A data frame
#' @param pattern A regex string to subset columns.
#' @param col_name Column name of the number of non-missing values for each row
#'
#' @return A data frame with new column containing the count of non-missing values for each column. By default, the new column
#' is called count_row_missing
#' @export
#'
#' @examples count_row_missing_values(mtcars)
count_row_non_missing <- function(df, pattern, col_name = "count_non_missing"){
  if (hasArg(pattern)){
    subset_df <- subset_column(df, pattern = pattern)
    df[[col_name]] <- rowSums(!is.na(subset_df))
  } else{
    df[[col_name
    ]] <- rowSums(!is.na(df))
  }
  return(df)
}
antchau/glider documentation built on Dec. 1, 2020, 9:23 p.m.