Skip to content

Instantly share code, notes, and snippets.

@ha0ye

ha0ye/dplyr-timing-test.R

Last active Jul 8, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.