TimTeaFan / dplyover

Create columns by applying functions to vectors and/or columns in 'dplyr'.
https://timteafan.github.io/dplyover/
Other
60 stars 1 forks source link

Bug in `accros2x` when used with a list of function and non-default `.comb`argument #23

Open abichat opened 7 months ago

abichat commented 7 months ago

Hello!

Thanks for your package, I use it a lot when I need to do complex computation! However, I found some weird behavior when we use several functions in accros2x() with a non-default option for .comb. Here's a reprex of things that could happen: wrong results, column that are computed but should not, and vice-versa. Hopefully, with .comb = "all", everything seems to be ok.

library(dplyr)
library(dplyover)

my_mtcars <-
  mtcars %>%
  as_tibble() %>%
  select(mpg, cyl, disp)

mtcars_md <-
  my_mtcars %>%
  mutate(across2x(.xcols = everything(),
                  .ycols = everything(),
                  .fns = list(m = ~ (.x + .y) / 2,
                              d = ~ .x - .y),
                  .comb = "unique"))

glimpse(mtcars_md)
#> Rows: 32
#> Columns: 15
#> $ mpg        <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2,…
#> $ cyl        <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4,…
#> $ disp       <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140…
#> $ mpg_mpg_m  <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2,…
#> $ mpg_mpg_d  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
#> $ mpg_cyl_m  <dbl> 13.50, 13.50, 13.40, 13.70, 13.35, 12.05, 11.15, 14.20, 13.…
#> $ mpg_cyl_d  <dbl> 15.0, 15.0, 18.8, 15.4, 10.7, 12.1, 6.3, 20.4, 18.8, 13.2, …
#> $ mpg_disp_m <dbl> 90.50, 90.50, 65.40, 139.70, 189.35, 121.55, 187.15, 85.55,…
#> $ mpg_disp_d <dbl> -139.0, -139.0, -85.2, -236.6, -341.3, -206.9, -345.7, -122…
#> $ cyl_mpg_d  <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4,…
#> $ cyl_cyl_m  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
#> $ cyl_cyl_d  <dbl> 83.00, 83.00, 56.00, 132.00, 184.00, 115.50, 184.00, 75.35,…
#> $ cyl_disp_m <dbl> -154.0, -154.0, -104.0, -252.0, -352.0, -219.0, -352.0, -14…
#> $ disp_mpg_d <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140…
#> $ disp_cyl_m <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
# cyl_mpg_d should not be computed 

mtcars_md$disp_cyl_m
#>  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# should not be 0

any((mtcars$disp - mtcars$mpg) == mtcars_md$disp_mpg_d)
#> [1] FALSE
# should be equal

my_mtcars %>%
  mutate(across2x(.xcols = everything(),
                  .ycols = everything(),
                  .fns = list(m = ~ (.x + .y) / 2,
                              d = ~ .x - .y),
                  .comb = "minimal")) %>%
  glimpse()
#> Rows: 32
#> Columns: 9
#> $ mpg        <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2,…
#> $ cyl        <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4,…
#> $ disp       <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140…
#> $ mpg_mpg_d  <dbl> 13.50, 13.50, 13.40, 13.70, 13.35, 12.05, 11.15, 14.20, 13.…
#> $ mpg_cyl_m  <dbl> 15.0, 15.0, 18.8, 15.4, 10.7, 12.1, 6.3, 20.4, 18.8, 13.2, …
#> $ mpg_cyl_d  <dbl> 90.50, 90.50, 65.40, 139.70, 189.35, 121.55, 187.15, 85.55,…
#> $ mpg_disp_m <dbl> -139.0, -139.0, -85.2, -236.6, -341.3, -206.9, -345.7, -122…
#> $ cyl_mpg_d  <dbl> 83.00, 83.00, 56.00, 132.00, 184.00, 115.50, 184.00, 75.35,…
#> $ cyl_cyl_m  <dbl> -154.0, -154.0, -104.0, -252.0, -352.0, -219.0, -352.0, -14…
# mpg_mpg_d/cyl_cyl_m should not be computed (and it's not the expected values)
# mpg_disp_d shoule be computed 

packageVersion("dplyover")
#> [1] '0.0.8.9002'

Created on 2024-02-22 with reprex v2.0.2

Thanks!