-
Notifications
You must be signed in to change notification settings - Fork 127
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
Unclear warnings and errors generated when setting levels for a factor generated from a character vector #314
Comments
Based on a quick read, I think you might be interested in |
Oh, this is perfect! Thank you for the pointer! I think this will solve my issue. level named argument is there, no errors or warnings if an additional level is listed but not in data, errors (unlike base::factor) if one of the supplied levels is not in the data. I'll close the issue and look forward to fct() getting into a future release. (example below if anyone curious). #setup ----
library(tidyverse)
fct <- function(x = character(), levels = NULL, na = character()) {
if (!is.character(x)) {
cli::cli_abort("{.arg x} must be a character vector")
}
if (!is.character(na)) {
cli::cli_abort("{.arg na} must be a character vector")
}
x[x %in% na] <- NA
if (is.null(levels)) {
levels <- unique(x)
} else if (!is.character(levels)) {
abort("`{.arg levels} must be a character vector")
}
invalid <- setdiff(x, c(levels, NA))
if (length(invalid) > 0 ) {
cli::cli_abort(c(
"Values of {.arg x} must be members of {.arg levels}",
i = "Invalid value{?s}: {.str {invalid}}"
))
}
factor(x, levels = levels, exclude = NULL)
}
mtcars2 <-
mtcars %>%
tibble::rownames_to_column(var = "make_model") %>%
dplyr::filter(
dplyr::row_number() <= 5
)
# Match levels----
match_levels <-
mtcars2 %>%
dplyr::pull(make_model)
mtcars2_factor <-
mtcars2 %>%
dplyr::mutate(
make_model = base::factor(
make_model,
levels = match_levels
)
)
mtcars2_fct <-
mtcars2 %>%
dplyr::mutate(
make_model = fct(
make_model,
levels = match_levels
)
)
# Add Levels ----
add_levels <-
c(match_levels, "Other Car")
mtcars2_add_factor <-
mtcars2 %>%
dplyr::mutate(
make_model = base::factor(
make_model,
levels = add_levels
)
)
mtcars2_add_fct <-
mtcars2 %>%
dplyr::mutate(
make_model = fct(
make_model,
levels = add_levels
)
)
levels(mtcars2_add_fct$make_model)
#> [1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
#> [4] "Hornet 4 Drive" "Hornet Sportabout" "Other Car"
# Miss Levels ----
miss_levels <-
match_levels[-1]
mtcars2_miss_factor <-
mtcars2 %>%
dplyr::mutate(
make_model = base::factor(
make_model,
levels = miss_levels
)
)
mtcars2_miss_fct <-
mtcars2 %>%
dplyr::mutate(
make_model = fct(
make_model,
levels = miss_levels
)
)
#> Error in `dplyr::mutate()`:
#> ! Problem while computing `make_model = fct(make_model, levels =
#> miss_levels)`.
#> Caused by error in `fct()`:
#> ! Values of `x` must be members of `levels`
#> i Invalid value: "Mazda RX4" Created on 2022-08-09 by the reprex package (v2.0.1) |
Also, if anyone runs into the same warning I got with fct_relevel (Warning: Outer names are only allowed for unnamed scalar atomic inputs), it's because you can't use the levels argument for that function; just pass the vector object of level names (in this case, use_levels) into the ellipsis on its own like:
|
When I use fct_relevel with the levels argument, I receive a warning that does not clearly indicate what is going wrong. Similarly, when I use the levels argument in forcats::as_factor()'s, (on the assumptions that arguments in .../ellipsis will be passed on to methods), I receive an error "Arguments in
...
must be used". Both of these are unexpected results for me based on my understanding of the function help text and base::factor().For background, my intention is to convert a character column into a factor column using a pre-specified list of levels (the pre-specified list is somewhat important as a check and consistency for reasons that I won't get into here). I have reviewed the forcats issues and don't see an exact match for this problem:
...
must be used." I am not clear if I am misusing the function.My questions are:
Reprex
Created on 2022-08-09 by the reprex package (v2.0.1)
Session info
The text was updated successfully, but these errors were encountered: