Skip to content

Instantly share code, notes, and snippets.

@jrosell
Last active March 20, 2024 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrosell/d3e02a80fa3c01032c31cc9c3caa6dc3 to your computer and use it in GitHub Desktop.
Save jrosell/d3e02a80fa3c01032c31cc9c3caa6dc3 to your computer and use it in GitHub Desktop.
How would you write this in base R 4.1+? Is there a better way? More approaches here: https://gist.github.com/hadley/c430501804349d382ce90754936ab8ec
# Single grouping in base R: Multiple functions
library(dplyr, warn.conflicts = FALSE)
expected_output <- mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(disp), n = n()) %>%
as.data.frame()
expected_output
by <- c("cyl")
result <- Reduce(function(df1, df2) merge(df1, df2, by = by), list(
aggregate(mtcars$disp, mtcars[by], mean) |> setNames(c(by, "mean")),
aggregate(mtcars$disp, mtcars[by], length) |> setNames(c(by, "n"))
))
result
testthat::expect_identical(result, expected_output)
# More complex grouping in base R: Multiple variariables and functions
library(dplyr, warn.conflicts = FALSE)
expected_output <- mtcars %>%
group_by(cyl, am) %>%
summarise(mean = mean(disp), n = n()) %>%
as.data.frame()
expected_output
by <- c("cyl", "am")
result <- Reduce(function(df1, df2) merge(df1, df2, by = by), list(
aggregate(mtcars$disp, mtcars[by], mean) |> setNames(c(by, "mean")),
aggregate(mtcars$mpg, mtcars[by], mean) |> setNames(c(by, "mpg")),
aggregate(mtcars$disp, mtcars[by], length) |> setNames(c(by, "n"))
))
result
testthat::expect_identical(result, expected_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment