tidyverse/forcats

Strange behaviour of `fct_count` (at least for beginners)

modche opened this issue · 1 comments

First of all, forcats is a really cool package, love it! Perhaps this might be improved:

library(tidyverse)
library(palmerpenguins)

I do not understand the difference between the (1) and the (3) example and I do not understand why (2) does not work. (At least the error message could be more informative).

> penguins %>%  count(species)
# A tibble: 3 × 2
  species       n
  <fct>     <int>
1 Adelie      152
2 Chinstrap    68
3 Gentoo      124


> penguins %>%  fct_count(species)
Error: `f` must be a factor (or character vector).


> fct_count(penguins$species)
# A tibble: 3 × 2
  f             n
  <fct>     <int>
1 Adelie      152
2 Chinstrap    68
3 Gentoo      124

-- Michael

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Big Sur 11.6.5

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] palmerpenguins_0.1.0 forcats_0.5.1        stringr_1.4.0        dplyr_1.0.8         
 [5] purrr_0.3.4          readr_2.1.2          tidyr_1.2.0          tibble_3.1.6        
 [9] ggplot2_3.3.5        tidyverse_1.3.1     

I think the best we could do here is

penguins %>%  fct_count(species)
#> Error: `f` must be a factor (or character vector), not a data frame.