Skip to content

Commit 61e22f4

Browse files
authored
Merge pull request #2597 from courtiol/count.sf
Add explicit methods for `dplyr::count()` and `dplyr:tally()`
2 parents a6dc902 + c91cb9c commit 61e22f4

File tree

6 files changed

+82
-1
lines changed

6 files changed

+82
-1
lines changed

DESCRIPTION

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ Authors@R:
4545
person(given = "Dewey",
4646
family = "Dunnington",
4747
role = "ctb",
48-
comment = c(ORCID = "0000-0002-9415-4582"))
48+
comment = c(ORCID = "0000-0002-9415-4582")),
49+
person(given = "Alexandre",
50+
family = "Courtiol",
51+
role = "ctb",
52+
comment = c(ORCID = "0000-0003-0637-2959"))
4953
)
5054
Description: Support for simple feature access, a standardized way to
5155
encode and analyze spatial vector data. Binds to 'GDAL'

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# version 1.1-1
22

3+
* `dplyr::count()` and `dplyr::tally()` no longer preserve geometries; #2596
4+
35
* better handle graticules crossing the antemeridian; #2561
46

57
* add the option `by_element = TRUE` to binary geometry predicates, measures and transformers; #2594 and #2595 by @rariariari w. help from Claude

R/tidyverse.R

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ slice.sf <- function(.data, ..., .dots) {
295295
#' nc.g |> summarise(mean(AREA))
296296
#' nc.g |> summarise(mean(AREA)) |> plot(col = grey(3:6 / 7))
297297
#' nc |> as.data.frame() |> summarise(mean(AREA))
298+
#' # counting geometries (after duplicating each row):
299+
#' nc.dupl <- nc[rep(seq_along(nc), each = 2), ]
300+
#' nc.dupl |> summarise(n = n(), .by = "geometry")
298301
#' }
299302
summarise.sf <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE) {
300303
sf_column = attr(.data, "sf_column")
@@ -341,6 +344,32 @@ summarise.sf <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE
341344
st_as_sf(structure(ret, sf_column = NULL))
342345
}
343346

347+
#' @name tidyverse
348+
#' @param wt see original function docs
349+
#' @param sort see original function docs
350+
#' @param name see original function docs
351+
#' @examples
352+
#' if (require(dplyr, quietly = TRUE)) {
353+
#' nc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25))
354+
#' nc |> count(area_cl)
355+
#' nc |> group_by(area_cl) |> tally()
356+
#' }
357+
#' @details The functions \code{count} and \code{tally} drop all geometries.
358+
#' For counting geometries use \code{summarise(.data, n = n(), .by = "geometry")}.
359+
count.sf <- function(x, ..., wt = NULL, sort = FALSE, name = "n") {
360+
x <- st_drop_geometry(x)
361+
if (!requireNamespace("dplyr", quietly = TRUE))
362+
stop("dplyr required: install that first") # nocov
363+
NextMethod()
364+
}
365+
366+
#' @name tidyverse
367+
tally.sf <- function(x, ..., wt = NULL, sort = FALSE, name = "n") {
368+
x <- st_drop_geometry(x)
369+
if (!requireNamespace("dplyr", quietly = TRUE))
370+
stop("dplyr required: install that first") # nocov
371+
NextMethod()
372+
}
344373

345374
#' @name tidyverse
346375
#' @param .keep_all see corresponding function in dplyr
@@ -692,6 +721,8 @@ register_all_s3_methods = function() {
692721
s3_register("dplyr::semi_join", "sf")
693722
s3_register("dplyr::slice", "sf")
694723
s3_register("dplyr::summarise", "sf")
724+
s3_register("dplyr::count", "sf")
725+
s3_register("dplyr::tally", "sf")
695726
s3_register("dplyr::transmute", "sf")
696727
s3_register("dplyr::ungroup", "sf")
697728
s3_register("tidyr::drop_na", "sf")

man/sf-package.Rd

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/tidyverse.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-tidyverse.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,23 @@ test_that("`pivot_wider()` works", {
338338
expect_identical(st_geometry(nc2), st_geometry(nc2_wider))
339339
expect_identical(st_drop_geometry(nc2), st_drop_geometry(nc2_wider))
340340
})
341+
342+
test_that("`count()` works", {
343+
skip_if_not_installed("dplyr")
344+
nc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25))
345+
unsorted <- count(nc, area_cl)
346+
sorted_and_named <- count(nc, area_cl, sort = TRUE, name = "number")
347+
expect_equal(unsorted$n, c(35, 15, 22, 28))
348+
expect_equal(sorted_and_named$number, c(35, 28, 22, 15))
349+
expect_false("sf" %in% class(unsorted))
350+
})
351+
352+
test_that("`tally()` works", {
353+
skip_if_not_installed("dplyr")
354+
nc$area_cl <- cut(nc$AREA, c(0, .1, .12, .15, .25))
355+
unsorted <- tally(group_by(nc, area_cl))
356+
sorted_and_named <- tally(group_by(nc, area_cl), sort = TRUE, name = "number")
357+
expect_equal(unsorted$n, c(35, 15, 22, 28))
358+
expect_equal(sorted_and_named$number, c(35, 28, 22, 15))
359+
expect_false("sf" %in% class(unsorted))
360+
})

0 commit comments

Comments
 (0)