groupMode: Find the most common value per group

View source: R/fillByGroup.R

groupModeR Documentation

Find the most common value per group

Description

Given a grouped data.frame, count values per group and return a vector with the most common value for each group. If there are several equally common values, the first will be chosen. Used by [fillByGroup()].

Usage

groupMode(
  df,
  cl,
  gp,
  new_cl = NA,
  min_n = NA,
  keep_first = TRUE,
  overwrite = FALSE
)

Arguments

df

a grouped tibble

cl

Name of column to find mode (character(1)).

gp

Name(s) of columns to group by

new_cl

Name of column to create. If NA (default), col is modified

min_n

(integer(1), default NA) Minimum number of occurrences of majority value. If provided, the majority value will be set to NA when it occurs less than min_n times.

keep_first

(logical(1), default TRUE) If there are multiple modes, should the first (in order of the data.frame) be selected? If FALSE, entries are set to NA

overwrite

(logical(1), default FALSE) If FALSE, new_cl will only replace NAs. If TRUE, new_cl will contain the mode. Note that keep_first = FALSE with overwrite = TRUE may lead to values being replaced with NA.


HelenLindsay/AbNames documentation built on June 6, 2023, 1:18 p.m.