Created
May 1, 2021 15:13
-
-
Save b-rodrigues/c7e4f777529e689f770f64f355f494fd 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
#code to the video: https://youtu.be/hs7q64diU_c | |
library(tidyverse) | |
# prepare johnson df | |
data(JohnsonJohnson) | |
johnson <- as_tibble(matrix(JohnsonJohnson, ncol = 4, nrow= 21, byrow = TRUE)) %>% | |
rename(quarter1 = V1, | |
quarter2 = V2, | |
quarter3 = V3, | |
quarter4 = V4) %>% | |
mutate(year = seq(1960, 1980)) | |
# the tidy way | |
# capture the expressions using quote | |
# for benchmarking | |
# you can remote the call to quote() to just | |
# get the data. but capturing the call allows | |
# me to benchmark it. | |
tidy_expr <- quote(johnson %>% | |
pivot_longer(cols = starts_with("quarter"), names_to = "quarter", values_to = "values") %>% | |
group_by(year) %>% | |
summarise(mean(values))) | |
# c_across solution | |
c_across_expr <- quote(johnson %>% | |
rowwise() %>% | |
mutate(year_mean = mean(c_across(starts_with("quarter"))))) | |
# "base" solution | |
base_expr <- quote(johnson %>% | |
mutate(year_mean = rowMeans(across(starts_with("quarter"))))) | |
# Now the benchmark | |
microbenchmark::microbenchmark( | |
eval(tidy_expr), | |
eval(c_across_expr), | |
eval(base_expr), | |
times = 100 | |
) | |
# Another example, with the EUSILC dataset | |
# here you actually want to work on a per row basis | |
options(pillar.sigfig = 10) | |
data("eusilcP", package = "simPop") | |
silc <- as_tibble(eusilcP) | |
silc | |
silc %>% | |
mutate(across(starts_with("py"), as.character)) %>% | |
head | |
silc %>% | |
rowwise() %>% | |
mutate(eq_sum = sum(c_across(starts_with("eq")))) %>% | |
select(starts_with("eq")) %>% | |
head | |
silc %>% | |
mutate(eq_sum = rowSums(across(starts_with("eq")))) %>% | |
select(starts_with("eq")) %>% | |
head |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment