Skip to content

Commit

Permalink
Add tally (#205)
Browse files Browse the repository at this point in the history
Fixes #201
  • Loading branch information
mgirlich authored Mar 2, 2021
1 parent 624331d commit ed026d7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3method(as_tibble,dtplyr_step)
S3method(auto_copy,dtplyr_step)
S3method(collect,dtplyr_step)
S3method(compute,dtplyr_step)
S3method(count,data.table)
S3method(count,dtplyr_step)
S3method(dim,dtplyr_step)
S3method(dim,dtplyr_step_first)
Expand Down Expand Up @@ -79,6 +80,8 @@ S3method(slice_tail,dtplyr_step)
S3method(summarise,data.table)
S3method(summarise,dtplyr_step)
S3method(tail,dtplyr_step)
S3method(tally,data.table)
S3method(tally,dtplyr_step)
S3method(tbl_vars,dtplyr_step)
S3method(tbl_vars,foo)
S3method(transmute,data.table)
Expand Down Expand Up @@ -135,6 +138,7 @@ importFrom(dplyr,slice_min)
importFrom(dplyr,slice_sample)
importFrom(dplyr,slice_tail)
importFrom(dplyr,summarise)
importFrom(dplyr,tally)
importFrom(dplyr,tbl_vars)
importFrom(dplyr,transmute)
importFrom(dplyr,ungroup)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# dtplyr (development version)

* Loading `dtplyr` doesn't break the `count()` method on data.tables anymore (@mgirlich, #201).

* `tally()` is now translated (@mgirlich, #201).

* More tidyr verbs are in the process of being added:

* `drop_na()` (@markfairbanks, #194)
Expand Down
24 changes: 21 additions & 3 deletions R/count.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ count.dtplyr_step <- function(.data, ..., wt = NULL, sort = FALSE, name = NULL)
out <- .data
}

wt <- enexpr(wt)
if (is.null(wt)) {
tally(out, wt = !!enquo(wt), sort = sort, name = name)
}

#' @export
count.data.table <- function(.data, ...) {
.data <- lazy_dt(.data)
count(.data, ...)
}

#' @importFrom dplyr tally
#' @export
tally.dtplyr_step <- function(.data, wt = NULL, sort = FALSE, name = NULL) {
wt <- enquo(wt)
if (quo_is_null(wt)) {
n <- expr(n())
} else {
n <- expr(sum(!!wt, na.rm = TRUE))
Expand All @@ -34,11 +46,17 @@ count.dtplyr_step <- function(.data, ..., wt = NULL, sort = FALSE, name = NULL)
abort("`name` must be a string")
}

out <- summarise(out, !!name := !!n)
out <- summarise(.data, !!name := !!n)

if (sort) {
out <- arrange(out, desc(!!sym(name)))
}

out
}

#' @export
tally.data.table <- function(.data, ...) {
.data <- lazy_dt(.data)
tally(.data, ...)
}
8 changes: 8 additions & 0 deletions tests/testthat/test-count.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ test_that("can sort", {
tibble(x = c(2, 1), n = c(10, 3))
)
})

test_that("tally works", {
dt <- lazy_dt(data.table(x = c(1, 1, 1, 2)), "DT")
expect_equal(
dt %>% group_by(x) %>% tally() %>% collect(),
tibble(x = c(1, 2), n = c(3, 1))
)
})

0 comments on commit ed026d7

Please sign in to comment.