Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added fct_lump_inorder() function #193

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(fct_infreq)
export(fct_inorder)
export(fct_inseq)
export(fct_lump)
export(fct_lump_inorder)
export(fct_lump_min)
export(fct_match)
export(fct_other)
Expand Down
52 changes: 52 additions & 0 deletions R/lump_inorder.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#' Lump together into "other" levels of a factor appearing after the N first (or prior to the N last)
#'
#' @param f A factor (or character vector).
#' @param n
#' If `n` is missing, `fct_lump_inorder` lumps
#' together all but the first appearing level into "other".
#'
#' Positive `n` preserves the first appearing `n` levels.
#' Negative `n` preserves the last appearing `n` levels.
#'
#' @param other_level Value of level used for "other" values.
#' @export
#' @seealso [fct_other()] to convert specified levels to other.
#' @seealso [fct_lump()] to convert low-frequency levels to other.
#' @examples
#' library(magrittr)
#' library(forcats)
#' f <- c("hello","goodbye","ciao","arrivederci","ciao","hello") %>% fct_inorder
#' f
#' #> [1] hello goodbye ciao arrivederci
#' #> [5] ciao hello
#' #> Levels: hello goodbye ciao arrivederci
#' # Preserve the first two appearing levels:
#' f %>% fct_lump_inorder(2)
#' #> [1] hello goodbye Other Other
#' #> [5] Other hello
#' #>Levels: hello goodbye Other
#' # Preserve the last two appearing levels, reversing order of levels:
#' f %>% fct_lump_inorder(-2)
#' #> [1] Other Other ciao arrivederci
#' #> [5] ciao Other
#' #>Levels: arrivederci ciao Other
#'
fct_lump_inorder <- function(f, n=1, other_level="Other") {
if (!is.factor(f)) f <- as.factor(f) # levels will be alphabetical
# if zero n or n is greater than f's levels, no lumping
if (n==0|(abs(n)>length(levels(f)))) return(f)
f_io <- fct_inorder(f) # levels ordered by appearance
if(n>0) {
levs <- levels(f_io)
fct_other(f_io,
keep=levs[1:n],
other_level=other_level)
}
else {
f_io <- fct_rev(f_io) # reverses level order
levs <- levels(f_io)
fct_other(f_io,
keep=levs[1:abs(n)], # n is negative
other_level=other_level)
}
}
3 changes: 2 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ R uses __factors__ to handle categorical variables, variables that have a fixed
* `fct_reorder()`: Reordering a factor by another variable.
* `fct_infreq()`: Reordering a factor by the frequency of values.
* `fct_relevel()`: Changing the order of a factor by hand.
* `fct_lump()`: Collapsing the least/most frequent values of a factor into "other".
* `fct_lump()`: Collapsing the least/most frequent levels of a factor into "other".
* `fct_lump_inorder():`: Collapsing the first/last appearing levels of a factor into "other".

You can learn more about each of these in `vignette("forcats")`. If you're new to factors, the best place to start is the [chapter on factors](http://r4ds.had.co.nz/factors.html) in R for Data Science.

Expand Down
47 changes: 47 additions & 0 deletions man/fct_lump_inorder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.