Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request @rsubset_rtransform #371

Open
Lincoln-Hannah opened this issue Nov 29, 2023 · 7 comments
Open

Request @rsubset_rtransform #371

Lincoln-Hannah opened this issue Nov 29, 2023 · 7 comments

Comments

@Lincoln-Hannah
Copy link

Apply an @rtransform block to a restricted set of rows. Fill other rows with existing value or missing for new columns.

@chain begin

    DataFrame( A = 1:4, B=[1,1,2,2] )

    @rsubset_rtransform (:B == 1) begin

        :A = 10
        :C = 20

    end

end


#result
DataFrame( A=[10,10,3,4], B=[1,1,2,2], C=[20,20,missing,missing])
@bkamins
Copy link
Member

bkamins commented Nov 29, 2023

Current:

julia> @chain DataFrame(A=1:4, B=[1,1,2,2]) begin
           @rsubset begin
               :B == 1
               @kwarg view=true
           end
           @rtransform! begin
               :A = 10
               :C = 20
           end
           parent
       end
4×3 DataFrame
 Row │ A      B      C
     │ Int64  Int64  Int64?
─────┼───────────────────────
   1 │    10      1       20
   2 │    10      1       20
   3 │     3      2  missing
   4 │     4      2  missing

@Lincoln-Hannah
Copy link
Author

Thanks Bogumil.
I'll probably lay it out as:

@chain DataFrame(A=1:4, B=[1,1,2,2]) begin

    @rsubset(:B == 1; view=true ); @rtransform! begin

        :A = 10
        :C = 20

    end; parent
    
end

I have 5 or so of these subset blocks within a larger @rtransform block. So don't want to have too many lines opening and closing each block.

@lincolnhannah
Copy link

Can you do the same with a group-by?
Have an @rtransform block for each value of :B
That might reduce the amount of code needed to begin. each block

@bkamins
Copy link
Member

bkamins commented Nov 29, 2023

@rsubset(:B == 1; view=true )

it does not work AFAICT. You need @kwarg but maybe please double check.

group-by should work, but could you please show the code and output you have in mind to make sure we are on the same page.?

@pdeffebach
Copy link
Collaborator

I absolutely want to implement this, ideally with the syntax

@rtransform df @when(:x == 1) :y = :z * 100

or

@rtransform df @when(:x == 1) begin
    :y = :z * 100
    :a = :b * 5
end

In the background we would have something along the lines of

@chain df begin 
    copy
    @rsubset(:x == 1; view = true)
    @rtransform! begin 
      :y = :z * 100
      :a = :b * 5
    end
    parent
end

This is very feasible, I just haven't written it.

@bkamins
Copy link
Member

bkamins commented Nov 29, 2023

@when is a good name I think.

@Lincoln-Hannah
Copy link
Author

That's awesome Peter. Shortens the code a lot.

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

No branches or pull requests

4 participants