Skip to content

Instantly share code, notes, and snippets.

@ha0ye
Last active July 8, 2019 04:00
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 ha0ye/aac845f85974f795d9009001d2b1f340 to your computer and use it in GitHub Desktop.
Save ha0ye/aac845f85974f795d9009001d2b1f340 to your computer and use it in GitHub Desktop.
library(dplyr)
library(microbenchmark)
set.seed(42)
n <- 10000
df <- data.frame(a = rep(seq(n), 2),
x = rnorm(2 * n))
group_summarize_mutate <- function(df)
{
df %>%
group_by(a) %>%
summarize(x = mean(x)) %>%
mutate(x = x / 100)
}
group_summarize <- function(df)
{
df %>%
group_by(a) %>%
summarize(x = mean(x) / 100)
}
mutate_group_summarize <- function(df)
{
df %>%
mutate(x = x / 100) %>%
group_by(a) %>%
summarize(x = mean(x))
}
microbenchmark(group_summarize_mutate(df),
group_summarize(df),
mutate_group_summarize(df),
times = 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment