Last active
July 8, 2019 04:00
-
-
Save ha0ye/aac845f85974f795d9009001d2b1f340 to your computer and use it in GitHub Desktop.
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
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