DavisVaughan · GitHub

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- tibble(x = 1:3)
# Why doesn't this recycle to size 0?
summarise(df, sum = sum(x), empty = tibble())
#> Error in `dplyr_col_modify()`:
#> ! Can't recycle `empty` (size 0) to size 1.
#> Backtrace:
#>     ▆
#>  1. ├─dplyr::summarise(df, sum = sum(x), empty = tibble())
#>  2. ├─dplyr:::summarise.data.frame(df, sum = sum(x), empty = tibble())
#>  3. │ └─dplyr:::summarise_build(.data, cols)
#>  4. │   ├─dplyr::dplyr_col_modify(out, cols$new)
#>  5. │   └─dplyr:::dplyr_col_modify.data.frame(out, cols$new)
#>  6. │     └─vctrs::vec_recycle_common(!!!cols, .size = nrow(data))
#>  7. └─vctrs:::stop_recycle_incompatible_size(...)
#>  8.   └─vctrs:::stop_vctrs(...)
#>  9.     └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = vctrs_error_call(call))
# This does
summarise(df, sum = sum(x), empty = tibble(a = integer()))
#> # A tibble: 0 × 2
#> # … with 2 variables: sum <int>, empty <tibble[,1]>
# And these work
summarise(df, sum = sum(x), tibble())
#> # A tibble: 1 × 1
#>     sum
#>   <int>
#> 1     6
summarise(df, sum = sum(x), tibble(a = integer()))
#> # A tibble: 0 × 2
#> # … with 2 variables: sum <int>, a <int>

Created on 2022-10-18 with reprex v2.0.2.9000

Read the original on github.com ↗