Skip to content

across() confuses function name with column name #6545

Closed

Description

@turbanisch

Apparently, any function whose name appears as a column name of a tibble cannot be used within across() even when it is specified as the .fns argument:

library(dplyr)

# column called "mean"
tibble(mean = 1:5) %>% 
  summarize(across(.fns = mean))
#> Error in `summarize()`:
#> ! Problem while computing `..1 = across(.fns = mean)`.
#> Caused by error:
#> ! attempt to select less than one element in integerOneIndex

#> Backtrace:
#>      ▆
#>   1. ├─tibble(mean = 1:5) %>% summarize(across(.fns = mean))
#>   2. ├─dplyr::summarize(., across(.fns = mean))
#>   3. ├─dplyr:::summarise.data.frame(., across(.fns = mean))
#>   4. │ └─dplyr:::summarise_cols(.data, dplyr_quosures(...), caller_env = caller_env())
#>   5. │   ├─base::withCallingHandlers(...)
#>   6. │   └─dplyr:::expand_across(dots[[i]])
#>   7. │     ├─dplyr:::across_setup(...)
#>   8. │     └─rlang::eval_tidy(expr$.fns, mask, env = env)
#>   9. ├─`<fn>`()
#>  10. └─base::.handleSimpleError(...)
#>  11.   └─dplyr (local) h(simpleError(msg, call))
#>  12.     └─rlang::abort(bullets, call = error_call, parent = skip_internal_condition(e))

# column called "average"
tibble(average = 1:5) %>% 
  summarize(across(.fns = mean))
#> # A tibble: 1 × 1
#>   average
#>     <dbl>
#> 1       3

Created on 2022-11-17 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.0 (2022-04-22)
#>  os       macOS 13.0.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Berlin
#>  date     2022-11-17
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/MacOS/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.2.0)
#>  cli           3.4.1   2022-09-23 [1] CRAN (R 4.2.0)
#>  DBI           1.1.3   2022-06-18 [1] CRAN (R 4.2.0)
#>  digest        0.6.30  2022-10-18 [1] CRAN (R 4.2.0)
#>  dplyr       * 1.0.10  2022-09-01 [1] CRAN (R 4.2.0)
#>  evaluate      0.18    2022-11-07 [1] CRAN (R 4.2.0)
#>  fansi         1.0.3   2022-03-24 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.2.0)
#>  fs            1.5.2   2021-12-08 [1] CRAN (R 4.2.0)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.3   2022-07-18 [1] CRAN (R 4.2.0)
#>  knitr         1.40    2022-08-24 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  pillar        1.8.1   2022-08-19 [1] CRAN (R 4.2.0)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.2.0)
#>  purrr         0.3.5   2022-10-06 [1] CRAN (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.2.0)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.0)
#>  rlang         1.0.6   2022-09-24 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.18    2022-11-09 [1] CRAN (R 4.2.0)
#>  rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.0)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>  stringi       1.7.8   2022-07-11 [1] CRAN (R 4.2.0)
#>  stringr       1.4.1   2022-08-20 [1] CRAN (R 4.2.0)
#>  styler        1.8.1   2022-11-07 [1] CRAN (R 4.2.0)
#>  tibble        3.1.8   2022-07-22 [1] CRAN (R 4.2.0)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.2.0)
#>  utf8          1.2.2   2021-07-24 [1] CRAN (R 4.2.0)
#>  vctrs         0.5.0   2022-10-22 [1] CRAN (R 4.2.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.34    2022-10-18 [1] CRAN (R 4.2.0)
#>  yaml          2.3.6   2022-10-18 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Activity

  1. haozhou1988 commented on Nov 18, 2022

    @haozhou1988

    It seems like .fns don't like the column name being same as the r base function... But change mean function as ~ mean(.) works well...

    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
    
    tibble(sd = 1:5) %>% 
      summarize(across(.fns = sd))
    #> Error in `summarize()`:
    #> ! Problem while computing `..1 = across(.fns = sd)`.
    #> Caused by error:
    #> ! attempt to select less than one element in integerOneIndex
    
    #> Backtrace:
    #>      ▆
    #>   1. ├─tibble(sd = 1:5) %>% summarize(across(.fns = sd))
    #>   2. ├─dplyr::summarize(., across(.fns = sd))
    #>   3. ├─dplyr:::summarise.data.frame(., across(.fns = sd))
    #>   4. │ └─dplyr:::summarise_cols(.data, dplyr_quosures(...), caller_env = caller_env())
    #>   5. │   ├─base::withCallingHandlers(...)
    #>   6. │   └─dplyr:::expand_across(dots[[i]])
    #>   7. │     ├─dplyr:::across_setup(...)
    #>   8. │     └─rlang::eval_tidy(expr$.fns, mask, env = env)
    #>   9. ├─`<fn>`()
    #>  10. └─base::.handleSimpleError(...)
    #>  11.   └─dplyr (local) h(simpleError(msg, call))
    #>  12.     └─rlang::abort(bullets, call = error_call, parent = skip_internal_condition(e))
    
    tibble(mean = 1:5) %>% 
      summarize(across(.fns = ~mean(.)))
    #> # A tibble: 1 × 1
    #>    mean
    #>   <dbl>
    #> 1     3

    Created on 2022-11-18 with reprex v2.0.2

  2. added a commit that references this issue on Nov 18, 2022
    e404b12
  3. added 2 commits that reference this issue on Dec 16, 2022
    44ec507
    0915029
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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