Skip to content

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #6696.

Essentially, stats::ave() created 0-length groups when used with a factor-grouping where some levels are absent. This then causes troubles for functions such as min() and max() that throw warnings when given 0-length data. We replace stats::ave() with the example from ?vec_chop. The vctrs-based solution is faster than stats::ave(), so I replaced all instances, not just the problematic one.

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2
#> Warning: package 'testthat' was built under R version 4.4.2

nrow(diamonds)
#> [1] 53940
bench::mark(
  ave(diamonds$carat, diamonds$color, FUN = mean),
  vec_ave(diamonds$carat, diamonds$color, mean), 
  min_iterations = 100
)
#> # A tibble: 2 × 6
#>   expression                             min median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                        <bch:tm> <bch:>     <dbl> <bch:byt>    <dbl>
#> 1 ave(diamonds$carat, diamonds$col…   2.16ms 2.41ms      389.    2.31MB     30.4
#> 2 vec_ave(diamonds$carat, diamonds…  998.8µs 1.15ms      830.    2.37MB     52.6

nrow(mtcars)
#> [1] 32
bench::mark(
  ave(mtcars$mpg, mtcars$cyl, FUN = mean),
  vec_ave(mtcars$mpg, mtcars$cyl, mean),
  min_iterations = 100
)
#> # A tibble: 2 × 6
#>   expression                            min  median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                        <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl>
#> 1 ave(mtcars$mpg, mtcars$cyl, FUN … 124.7µs 135.4µs     6997.    1.48KB     6.19
#> 2 vec_ave(mtcars$mpg, mtcars$cyl, …  29.5µs  32.5µs    28604.    1.04KB     8.58

Created on 2025-10-13 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected warnings in geom_tile() when using facets and external data

1 participant