collapse icon indicating copy to clipboard operation
collapse copied to clipboard

[BUG] Group by + summarise on 0-row tibble produces invalid output.

Open kendonB opened this issue 8 months ago • 1 comments

Describe the bug Group by + summarise on 0-row tibble causes the summarised variable to be invalid.

Steps/Code to Reproduce

library(tidyverse)
library(collapse)
#> Warning: package 'collapse' was built under R version 4.4.2
#> collapse 2.0.17, see ?`collapse-package` or ?`collapse-documentation`
#> 
#> Attaching package: 'collapse'
#> The following object is masked from 'package:lubridate':
#> 
#>     is.Date
#> The following object is masked from 'package:tidyr':
#> 
#>     replace_na
#> The following object is masked from 'package:stats':
#> 
#>     D
no_rows_tibble <- tibble::tibble(var_1 = character(), var_2 = numeric())
tmp <- no_rows_tibble |>
  collapse::fgroup_by(var_1) |>
  collapse::fsummarise(var_2 = sum(var_2))
length(tmp)
#> [1] 2
class(tmp[[2]])
#> [1] "NULL"

tmp2 <- no_rows_tibble |>
  dplyr::group_by(var_1) |>
  dplyr::summarise(var_2 = sum(var_2))
length(tmp2)
#> [1] 2
class(tmp2[[2]])
#> [1] "numeric"

Created on 2025-04-23 with reprex v2.1.1

Expected behavior class of the summarised variable should remain numeric

Additional context Also reported here as it creates a vctrs internal error.

kendonB avatar Apr 23 '25 00:04 kendonB

Thanks, I just observe the bug doesn't occur with fsum(), which you should be using if you care about performance with collapse.

SebKrantz avatar Apr 23 '25 09:04 SebKrantz