Hello! I found out that summarising logical variable over a grouped tibble somehow takes A LOT more time than summarising double variable. Don't really sure if is is a dplyr problem or not, but this situation seems counter-intuitive for me.
Here is a simple examble, where summarising logical variable is 100 times slower than double:
library(dplyr)
library(microbenchmark)
# create test tibble with logical and double variables
testTibble <- tibble(v = 1:10000 %>% rep(5),
a = 1,
b = TRUE,
c = 1L) %>%
group_by(v)
testTibble %>% glimpse()
#> Observations: 50,000
#> Variables: 4
#> $ v <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1...
#> $ a <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
#> $ b <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, ...
#> $ c <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
microbenchmark(
# summarizing double variable
testTibble %>% summarise(s = sum(a)),
# summarizing logical variable <- ISSUE IS HERE
testTibble %>% summarise(s = sum(b)),
# summarizing integer variable
testTibble %>% summarise(s = sum(c)),
# compare with simple sums without grouping
testTibble %>% pull(a) %>% sum(),
testTibble %>% pull(b) %>% sum(),
testTibble %>% pull(c) %>% sum()
)
#> Unit: milliseconds
#> expr min lq mean
#> testTibble %>% summarise(s = sum(a)) 1.872467 2.049959 2.217249
#> testTibble %>% summarise(s = sum(b)) 268.395932 272.521836 277.941838
#> testTibble %>% summarise(s = sum(c)) 1.938750 2.108582 2.296201
#> testTibble %>% pull(a) %>% sum() 3.053631 3.165402 3.328143
#> testTibble %>% pull(b) %>% sum() 3.101226 3.165035 3.315196
#> testTibble %>% pull(c) %>% sum() 3.066030 3.157654 3.329642
#> median uq max neval
#> 2.169340 2.300528 3.591446 100
#> 273.824589 279.503995 335.747118 100
#> 2.195381 2.336596 6.696211 100
#> 3.233151 3.363945 4.896729 100
#> 3.251053 3.381626 5.105280 100
#> 3.253313 3.391950 4.732548 100
Hello! I found out that summarising logical variable over a grouped tibble somehow takes A LOT more time than summarising double variable. Don't really sure if is is a dplyr problem or not, but this situation seems counter-intuitive for me.
Here is a simple examble, where summarising logical variable is 100 times slower than double:
Session info