diff --git a/r/R/dplyr.R b/r/R/dplyr.R index 845cb3a1815..c172e9ba065 100644 --- a/r/R/dplyr.R +++ b/r/R/dplyr.R @@ -632,7 +632,7 @@ arrow_mask <- function(.data) { # Some R functions will still try to evaluate on an Expression # and return NA with a warning fail <- function(...) stop("Not implemented") - for (f in c("mean")) { + for (f in c("mean", "sd")) { f_env[[f]] <- fail } @@ -1006,7 +1006,6 @@ abandon_ship <- function(call, .data, msg = NULL) { stop(msg, "\nCall collect() first to pull data into R.", call. = FALSE) } } - # else, collect and call dplyr method if (!is.null(msg)) { warning(msg, "; pulling data into R", immediate. = TRUE, call. = FALSE) diff --git a/r/src/compute.cpp b/r/src/compute.cpp index 34bc3bea456..c215d661e3a 100644 --- a/r/src/compute.cpp +++ b/r/src/compute.cpp @@ -233,6 +233,11 @@ std::shared_ptr make_compute_options( max_replacements); } + if (func_name == "variance" || func_name == "stddev") { + using Options = arrow::compute::VarianceOptions; + return std::make_shared(cpp11::as_cpp(options["ddof"])); + } + return nullptr; } diff --git a/r/tests/testthat/test-compute-aggregate.R b/r/tests/testthat/test-compute-aggregate.R index 0621b7779c7..398b39fb17f 100644 --- a/r/tests/testthat/test-compute-aggregate.R +++ b/r/tests/testthat/test-compute-aggregate.R @@ -381,3 +381,21 @@ test_that("all.Array and all.ChunkedArray", { expect_vector_equal(all(input, na.rm = TRUE), data_logical) }) + +test_that("variance", { + data <- c(-37, 267, 88, -120, 9, 101, -65, -23, NA) + arr <- Array$create(data) + chunked_arr <- ChunkedArray$create(data) + + expect_equal(call_function("variance", arr, options = list(ddof = 5)), Scalar$create(34596)) + expect_equal(call_function("variance", chunked_arr, options = list(ddof = 5)), Scalar$create(34596)) +}) + +test_that("stddev", { + data <- c(-37, 267, 88, -120, 9, 101, -65, -23, NA) + arr <- Array$create(data) + chunked_arr <- ChunkedArray$create(data) + + expect_equal(call_function("stddev", arr, options = list(ddof = 5)), Scalar$create(186)) + expect_equal(call_function("stddev", chunked_arr, options = list(ddof = 5)), Scalar$create(186)) +})