markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
450 stars 32 forks source link

summarize with across behaves in a strange way and gives an error when I use lapply inside #798

Open bnicenboim opened 9 months ago

bnicenboim commented 9 months ago

These two lines give identical results, but the second one leads to an error in the call:

list(a = 1, b = 2, c = 3, d = 4) %>% unlist()
#> a b c d 
#> 1 2 3 4
lapply(c(a = 1, b = 2, c = 3, d = 4), function(b) b[[1]]) %>% unlist()
#> a b c d 
#> 1 2 3 4

See here:

library(tidytable)
.data <- tidytable(x = 1:10, y = 1)
.data %>%
  summarize(across(
    all_of("x"),
    function(y) {
      list(a = 1, b = 2, c = 3, d = 4) %>% unlist()
    }
  ), .by = "y")
#> # A tidytable: 4 × 2
#>       y     x
#>   <dbl> <dbl>
#> 1     1     1
#> 2     1     2
#> 3     1     3
#> 4     1     4

.data %>%
  summarize(across(
    all_of("x"),
    function(y) {
      lapply(c(a = 1, b = 2, c = 3, d = 4), function(b) b[[1]]) %>% unlist()
    }
  ), .by = "y")
#> Error in replace_dot_alias(e[[i]]): badly formed function expression

Created on 2024-01-26 with reprex v2.0.2