Skip to content

Commit

Permalink
Ensure count() adds grouping vars
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Mar 14, 2016
1 parent 7219eaa commit a42dda3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dplyr 0.4.3.9000

* `count()` now adds additional grouping variables, rather than overriding
existing (#1703).

* `pmin()` and `pmax()` are translated to SQL `MIN()` and `MAX()` (#1711).

* `case_when()` is a general vectorised if + else if (#631).
Expand Down
2 changes: 1 addition & 1 deletion R/tally.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ count <- function(x, ..., wt = NULL, sort = FALSE) {
#' @export
#' @rdname tally
count_ <- function(x, vars, wt = NULL, sort = FALSE) {
grouped <- group_by_(x, .dots = vars)
grouped <- group_by_(x, .dots = vars, add = TRUE)
tally_(grouped, wt = wt, sort = sort)
}
8 changes: 8 additions & 0 deletions tests/testthat/test-count.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ test_that("can count variable called n", {
expect_equal(out$nn, c(3, 2))
})

test_that("grouped count includes group", {
df <- data.frame(g = c(1, 2, 2, 2))

res <- df %>% group_by(g) %>% count()
expect_equal(names(res), c("g", "n"))
expect_equal(res$n, c(1, 3))
})

# n_distinct --------------------------------------------------------------

test_that("count_distinct gives the correct results on iris", {
Expand Down

0 comments on commit a42dda3

Please sign in to comment.