Skip to content

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

@kendonB

Description

@kendonB

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions