Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R >=4.1 function notation \(x) throws error for group_map() (purrr package)

Tags:

r

purrr

I want to use the short notation for functions in R version 4.1 and higher \(x) x+1 (as encouraged by the tidyverse developers) with group_map() from the purrr package. It doesn't seem to work or do I do something wrong?

library(purrr)

# old notation works
mtcars |>
  group_by(cyl) |>
  group_map(~ lm(mpg ~ disp + hp, data = .x))


# R version >=4.1 notation does not work
mtcars |>
  group_by(cyl) |>
  group_map(\(x) lm(mpg ~ disp + hp, data = x))`your text`

Created on 2023-08-19 with reprex v2.0.2

Session info

sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 10 x64 (build 17763)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  date     2023-08-19
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date (UTC) lib source
#>  purrr       * 1.0.2   2023-08-10 [1] CRAN (R 4.3.1)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.3.0)
#> 
#>  [1] C:/Users/te/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.1/library
#> 
#> ------------------------------------------------------------------------------
like image 516
AGeographer Avatar asked Jun 25 '26 09:06

AGeographer


1 Answers

The function used to map groups needs to accomodate two arguments (data and group), regardless of notation. Try:

mtcars |>
  group_by(cyl) |>
  group_map(\(x, y) lm(mpg ~ disp + hp, data = x))
like image 166
I_O Avatar answered Jun 28 '26 03:06

I_O



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!