Skip to content

Conversation

drizk1
Copy link
Member

@drizk1 drizk1 commented Aug 14, 2025

Did some experimenting to add support for multiple function args in an across function and with relatively limited changes to parse_across managed to get it to work. suffix corresponds to function order in across. However, I fully expect there is a different way to do this under the hood

julia> using TidierData, TidierFiles

julia> mtcars = read_file( "https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv");

julia> wt_mean(x,y) = x' * y / sum(y);

julia> wt_mean2(x,y,n) = x' * y / sum(y) + n;

julia> @chain mtcars begin
         @group_by(cyl)
         @summarize(across((!(wt, cyl, model)), (x -> wt_mean(x, wt))))
       end
3×10 DataFrame
 Row │ cyl    mpg_1    disp_1   hp_1      drat_1   qsec_1   vs_1      am_1      gear_1   carb_1  
     │ Int64  Float64  Float64  Float64   Float64  Float64  Float64   Float64   Float64  Float64 
─────┼───────────────────────────────────────────────────────────────────────────────────────────
   16  19.6458  185.239  121.558   3.56917  18.122   0.621219  0.378781  3.82104  3.33616
   24  25.935   110.35    83.3945  4.03126  19.3804  0.914887  0.649803  4.04725  1.57225
   38  14.8064  362.125  209.373   3.20566  16.8853  0.0       0.120381  3.24076  3.53955

julia> @chain mtcars begin
         @group_by(cyl)
         @summarize(across((!(wt, cyl, model)), (x -> wt_mean(x, wt), x -> wt_mean2(x, wt, 5), minimum)))
       end
3×28 DataFrame
 Row │ cyl    mpg_1    mpg_2    mpg_3    disp_1   disp_2   disp_3   hp_1      hp_2      hp_3   drat_1   drat_2   d 
     │ Int64  Float64  Float64  Float64  Float64  Float64  Float64  Float64   Float64   Int64  Float64  Float64  F 
─────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────
   16  19.6458  24.6458     17.8  185.239  190.239    145.0  121.558   126.558     105  3.56917  8.56917    
   24  25.935   30.935      21.4  110.35   115.35      71.1   83.3945   88.3945     52  4.03126  9.03126
   38  14.8064  19.8064     10.4  362.125  367.125    275.8  209.373   214.373     150  3.20566  8.20566
                                                                                                  16 columns omitted
> mtcars %>%
+   group_by(cyl) %>% 
+   summarize(across(-wt, ~weighted.mean(., wt)))
# A tibble: 3 × 11
    cyl   mpg  disp    hp  drat  qsec    vs    am  gear  carb weight
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl>
1     4  25.9  110.  83.4  4.03  19.4 0.915 0.650  4.05  1.57  0.483
2     6  19.6  185. 122.   3.57  18.1 0.621 0.379  3.82  3.34  0.429
3     8  14.8  362. 209.   3.21  16.9 0     0.120  3.24  3.54  0.512

edit: that failing nightly test has to do w column order getting changed in an unnest. not sure if this is something that needs to be treated as a bug, or resolved with a @relocate. in a dif PR i just resovled w a @relocate

@kdpsingh
Copy link
Member

Awesome @drizk1, I will take a look at this and the other PRs.

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.

2 participants