-
Notifications
You must be signed in to change notification settings - Fork 301
Open
Description
Syntax in dplyr allows to use a .by
argument with in summarize()
as an alternative to using a group_by()
function before summarize()
. In the example below, I would expect the two pipelines to yield the same results and no error.
library(sf)
#> Linking to GEOS 3.13.1, GDAL 3.10.2, PROJ 9.5.1; sf_use_s2() is TRUE
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
nc = read_sf(system.file("shape/nc.shp", package="sf"))
nc_area <-
nc %>%
mutate(
area_cl = cut(AREA, c(0, .1, .12, .15, .25))
)
nc_area %>%
group_by(area_cl) %>%
summarize(
across(c(AREA, BIR74, BIR79, SID74, SID79), sum),
NCounty=n()
)
#> Simple feature collection with 4 features and 7 fields
#> Geometry type: MULTIPOLYGON
#> Dimension: XY
#> Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS: NAD27
#> # A tibble: 4 × 8
#> area_cl AREA BIR74 BIR79 SID74 SID79 NCounty geometry
#> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <MULTIPOLYGON [°]>
#> 1 (0,0.1] 2.66 62238 80242 120 179 35 (((-76.68874 36.29452, -7…
#> 2 (0.1,0.12] 1.68 46196 57382 79 115 15 (((-80.93127 35.61959, -8…
#> 3 (0.12,0.15] 2.95 77364 100376 161 180 22 (((-76.46926 34.69328, -7…
#> 4 (0.15,0.25] 5.33 144164 184392 307 362 28 (((-80.76612 35.68204, -8…
nc_area %>%
summarize(
across(c(AREA, BIR74, BIR79, SID74, SID79), sum),
NCounty=n(),
.by="area_cl"
)
#> Error in summarise.sf(., across(c(AREA, BIR74, BIR79, SID74, SID79), sum), : when using .by, also add across(geometry, st_union) as argument
Created on 2025-08-10 with reprex v2.1.1
Session info
sessioninfo::session_info()
#> Warning in system2("quarto", "-V", stdout = TRUE, env = paste0("TMPDIR=", :
#> running command '"quarto"
#> TMPDIR=C:/Users/steph/AppData/Local/Temp/RtmpG4eCRs/file269046a0a05 -V' had
#> status 1
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.5.1 (2025-06-13 ucrt)
#> os Windows 11 x64 (build 26100)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.utf8
#> ctype English_United States.utf8
#> tz America/New_York
#> date 2025-08-10
#> pandoc 3.5 @ C:/Users/steph/AppData/Local/Pandoc/ (via rmarkdown)
#> quarto NA @ C:\\Users\\steph\\AppData\\Local\\Programs\\Quarto\\bin\\quarto.exe
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> class 7.3-23 2025-01-01 [2] CRAN (R 4.5.1)
#> classInt 0.4-11 2025-01-08 [1] RSPM (R 4.5.0)
#> cli 3.6.4 2025-02-13 [1] RSPM
#> DBI 1.2.3 2024-06-02 [1] RSPM
#> digest 0.6.37 2024-08-19 [1] RSPM
#> dplyr * 1.1.4 2023-11-17 [1] RSPM
#> e1071 1.7-16 2024-09-16 [1] RSPM (R 4.5.0)
#> evaluate 1.0.3 2025-01-10 [1] RSPM
#> fastmap 1.2.0 2024-05-15 [1] RSPM
#> fs 1.6.6 2025-04-12 [1] RSPM
#> generics 0.1.3 2022-07-05 [1] RSPM
#> glue 1.8.0 2024-09-30 [1] RSPM
#> htmltools 0.5.8.1 2024-04-04 [1] RSPM
#> KernSmooth 2.23-26 2025-01-01 [2] CRAN (R 4.5.1)
#> knitr 1.50 2025-03-16 [1] RSPM
#> lifecycle 1.0.4 2023-11-07 [1] RSPM
#> magrittr 2.0.3 2022-03-30 [1] RSPM
#> pillar 1.10.2 2025-04-05 [1] RSPM
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM
#> proxy 0.4-27 2022-06-09 [1] RSPM (R 4.5.0)
#> R6 2.6.1 2025-02-15 [1] RSPM
#> Rcpp 1.0.14 2025-01-12 [1] RSPM
#> reprex 2.1.1 2024-07-06 [1] RSPM
#> rlang 1.1.6 2025-04-11 [1] RSPM (R 4.5.0)
#> rmarkdown 2.29 2024-11-04 [1] RSPM
#> rstudioapi 0.17.1 2024-10-22 [1] RSPM
#> s2 1.1.9 2025-05-23 [1] RSPM (R 4.5.0)
#> sessioninfo 1.2.3 2025-02-05 [1] RSPM (R 4.5.0)
#> sf * 1.0-21 2025-05-15 [1] RSPM (R 4.5.0)
#> tibble 3.2.1 2023-03-20 [1] RSPM
#> tidyselect 1.2.1 2024-03-11 [1] RSPM
#> units 0.8-7 2025-03-11 [1] RSPM (R 4.5.0)
#> utf8 1.2.4 2023-10-22 [1] RSPM
#> vctrs 0.6.5 2023-12-01 [1] RSPM
#> withr 3.0.2 2024-10-28 [1] RSPM
#> wk 0.9.4 2024-10-11 [1] RSPM (R 4.5.0)
#> xfun 0.52 2025-04-02 [1] RSPM
#> yaml 2.3.10 2024-07-26 [1] RSPM
#>
#> [1] C:/Users/steph/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
Labels
No labels