Last active
March 20, 2024 15:44
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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