mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2026-06-19 05:07:30 +02:00
fct_drop refined
This commit is contained in:
parent
f22a0a56b2
commit
7d82eeebd4
8 changed files with 92 additions and 17 deletions
24
R/fct_drop.R
24
R/fct_drop.R
|
|
@ -1,7 +1,7 @@
|
|||
#' Drop unused levels preserving label data
|
||||
#'
|
||||
#' This extends [forcats::fct_drop()] to natively work across a data.frame and
|
||||
#' replace [base::droplevels()].
|
||||
#' replaces [base::droplevels()].
|
||||
#'
|
||||
#' @param x Factor to drop unused levels
|
||||
#' @param ... Other arguments passed down to method.
|
||||
|
|
@ -10,13 +10,20 @@
|
|||
#' @importFrom forcats fct_drop
|
||||
#' @export
|
||||
#' @name fct_drop
|
||||
NULL
|
||||
fct_drop <- function(x, ...) {
|
||||
UseMethod("fct_drop")
|
||||
}
|
||||
|
||||
#' @rdname fct_drop
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' mtcars |>
|
||||
#' numchar2fct() |>
|
||||
#' fct_drop()
|
||||
fct_drop.data.frame <- function(x, ...) {
|
||||
purrr::map(x, \(.x){
|
||||
if (is.factor(.x)){
|
||||
if (is.factor(.x)) {
|
||||
forcats::fct_drop(.x)
|
||||
} else {
|
||||
.x
|
||||
|
|
@ -26,4 +33,13 @@ fct_drop.data.frame <- function(x, ...) {
|
|||
}
|
||||
|
||||
|
||||
|
||||
#' @rdname fct_drop
|
||||
#' @export
|
||||
#'
|
||||
#' @examples
|
||||
#' mtcars |>
|
||||
#' numchar2fct() |>
|
||||
#' dplyr::mutate(vs = fct_drop(vs))
|
||||
fct_drop.factor <- function(x, ...) {
|
||||
forcats::fct_drop(f = x, ...)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue