Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion r/R/dplyr-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,6 @@ agg_funcs$all <- function(x, na.rm = FALSE) {
options = list(na.rm = na.rm, na.min_count = 0L)
)
}

agg_funcs$mean <- function(x, na.rm = FALSE) {
list(
fun = "mean",
Expand All @@ -825,6 +824,15 @@ agg_funcs$var <- function(x, na.rm = FALSE, ddof = 1) {
options = list(ddof = ddof)
)
}

agg_funcs$n_distinct <- function(x, na.rm = FALSE) {
list(
fun = "count_distinct",
data = x,
options = list(na.rm = na.rm)
)
}

agg_funcs$n <- function() {
list(
fun = "sum",
Expand Down
8 changes: 8 additions & 0 deletions r/src/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ std::shared_ptr<arrow::compute::FunctionOptions> make_compute_options(
return out;
}

if (func_name == "hash_count_distinct") {
using Options = arrow::compute::CountOptions;
auto out = std::make_shared<Options>(Options::Defaults());
out->mode =
cpp11::as_cpp<bool>(options["na.rm"]) ? Options::ONLY_VALID : Options::ALL;
return out;
}

if (func_name == "min_element_wise" || func_name == "max_element_wise") {
using Options = arrow::compute::ElementWiseAggregateOptions;
bool skip_nulls = true;
Expand Down
17 changes: 17 additions & 0 deletions r/tests/testthat/test-dplyr-aggregate.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,23 @@ test_that("Group by any/all", {
)
})

test_that("Group by n_distinct() on dataset", {
expect_dplyr_equal(
input %>%
group_by(some_grouping) %>%
summarize(distinct = n_distinct(lgl, na.rm = FALSE)) %>%
collect(),
tbl
)
expect_dplyr_equal(
input %>%
group_by(some_grouping) %>%
summarize(distinct = n_distinct(lgl, na.rm = TRUE)) %>%
collect(),
tbl
)
})

test_that("Filter and aggregate", {
expect_dplyr_equal(
input %>%
Expand Down