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

fct_reorder2() drops values #348

Open
hadley opened this issue Feb 27, 2023 · 1 comment
Open

fct_reorder2() drops values #348

hadley opened this issue Feb 27, 2023 · 1 comment
Labels
bug an unexpected problem or unintended behavior

Comments

@hadley
Copy link
Member

hadley commented Feb 27, 2023

library(forcats)
library(dplyr, warn.conflicts = FALSE)

by_age <- gss_cat |>
  count(age, marital) |>
  group_by(age) |>
  mutate(
    prop = n / sum(n)
  )

nrow(by_age)
#> [1] 357
with(by_age, length(fct_reorder2(marital, age, prop, .na_rm = TRUE)))
#> [1] 351
with(by_age, length(fct_reorder2(marital, age, prop)))
#> Warning: `fct_reorder2()` removing 6 missing values.
#> ℹ Use `.na_rm = TRUE` to silence this message.
#> ℹ Use `.na_rm = FALSE` to preserve NAs.
#> [1] 351

Created on 2023-02-27 with reprex v2.0.2

@hadley hadley added the bug an unexpected problem or unintended behavior label Oct 31, 2023
@DanChaltiel
Copy link

Note that the warning is not thrown inside a mutate call:

library(forcats)
library(dplyr, warn.conflicts = FALSE)

by_age <- gss_cat |>
  count(age, marital) |>
  mutate(
    prop = n / sum(n), 
    .by = age
  )

by_age |>
  mutate(m = fct_reorder2(marital, age, prop)) |>
  nrow()
#> Error in `mutate()`:
#> ℹ In argument: `m = fct_reorder2(marital, age, prop)`.
#> Caused by error:
#> ! `m` must be size 357 or 1, not 351.

by_age |>
  mutate(m = fct_reorder2(marital, age, prop, .na_rm = FALSE)) |>
  nrow()
#> [1] 357

Created on 2024-05-14 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants