tidyverse / forcats

🐈🐈🐈🐈: tools for working with categorical variables (factors)
https://forcats.tidyverse.org/
Other
553 stars 126 forks source link

Errors in `fct_reorder2` documentation #352

Open AlexJHomer opened 1 year ago

AlexJHomer commented 1 year ago

https://github.com/tidyverse/forcats/blob/4a8525abba44dbd4f9a03efff09c85922039ffa3/R/reorder.R#L9-L11

The phrase "ascending order" is misleading, since for fct_reorder2 the default is to use descending order instead, and in any case the direction is controlled by the .desc argument.

f <- factor(c("a", "a", "b"))
f
#> [1] a a b
#> Levels: a b

x <- c(1, 2, 3)
y <- c(2, 4, 6)
fun <- \(x, y) mean(x + y)

# fun gives a lower value for "a" than for "b":
fun(x[f == "a"], y[f == "a"])
#> [1] 4.5
fun(x[f == "b"], y[f == "b"])
#> [1] 9

forcats::fct_reorder2(f, x, y, fun)
#> [1] a a b
#> Levels: b a

Created on 2023-06-24 with reprex v2.0.2

Less crucially, it should read `.fun(.x, .y)` instead of `fun(.x, .y)`.