Skip to content

count function needs guard rails that name shouldn't be an existing column #7773

@szimmer

Description

@szimmer

If you use the count function and set name to be one of the existing columns you are tabulating on, you get unexpected results. This should yield either an error or a note that the name had to be modified.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
mydat <- tibble(x=rep(c(1,2), 5))
mydat |>
  count(x)
#> # A tibble: 2 × 2
#>       x     n
#>   <dbl> <int>
#> 1     1     5
#> 2     2     5
mydat |>
  count(x, name="x")
#> # A tibble: 2 × 1
#>       x
#>   <int>
#> 1     5
#> 2     5

Created on 2025-11-20 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.5.1 (2025-06-13 ucrt)
#>  os       Windows 11 x64 (build 22631)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United States.utf8
#>  ctype    English_United States.utf8
#>  tz       America/New_York
#>  date     2025-11-20
#>  pandoc   3.6.3 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#>  quarto   1.8.24 @ C:\\Users\\sazimmer\\AppData\\Local\\Programs\\Quarto\\bin\\quarto.exe
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  cli           3.6.5      2025-04-23 [1] CRAN (R 4.5.0)
#>  digest        0.6.37     2024-08-19 [1] CRAN (R 4.5.0)
#>  dplyr       * 1.1.4      2023-11-17 [1] CRAN (R 4.5.0)
#>  evaluate      1.0.5      2025-08-27 [1] RSPM (R 4.5.0)
#>  fastmap       1.2.0      2024-05-15 [1] CRAN (R 4.5.0)
#>  fs            1.6.6      2025-04-12 [1] CRAN (R 4.5.0)
#>  generics      0.1.4      2025-05-09 [1] CRAN (R 4.5.0)
#>  glue          1.8.0      2024-09-30 [1] CRAN (R 4.5.0)
#>  htmltools     0.5.8.1    2024-04-04 [1] CRAN (R 4.5.0)
#>  knitr         1.50       2025-03-16 [1] CRAN (R 4.5.0)
#>  lifecycle     1.0.4      2023-11-07 [1] CRAN (R 4.5.0)
#>  magrittr      2.0.3      2022-03-30 [1] CRAN (R 4.5.0)
#>  pillar        1.11.0     2025-07-04 [1] RSPM (R 4.5.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 4.5.0)
#>  R6            2.6.1      2025-02-15 [1] CRAN (R 4.5.0)
#>  reprex        2.1.1      2024-07-06 [1] CRAN (R 4.5.0)
#>  rlang         1.1.6      2025-04-11 [1] CRAN (R 4.5.0)
#>  rmarkdown     2.29       2024-11-04 [1] CRAN (R 4.5.0)
#>  rstudioapi    0.17.1     2024-10-22 [1] CRAN (R 4.5.0)
#>  sessioninfo   1.2.3.9000 2025-09-18 [1] Github (r-lib/sessioninfo@ec4dd0c)
#>  tibble        3.3.0      2025-06-08 [1] RSPM (R 4.5.0)
#>  tidyselect    1.2.1      2024-03-11 [1] CRAN (R 4.5.0)
#>  vctrs         0.6.5      2023-12-01 [1] CRAN (R 4.5.0)
#>  withr         3.0.2      2024-10-28 [1] CRAN (R 4.5.0)
#>  xfun          0.53       2025-08-19 [1] RSPM (R 4.5.0)
#>  yaml          2.3.10     2024-07-26 [1] CRAN (R 4.5.0)
#> 
#>  [1] C:/Users/sazimmer/AppData/Local/R/win-library/4.5
#>  [2] C:/Program Files/R/R-4.5.1/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions