Skip to content

@replace for in-place conditional updating #144

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

Open
aaowens opened this issue Feb 8, 2020 · 1 comment
Open

@replace for in-place conditional updating #144

aaowens opened this issue Feb 8, 2020 · 1 comment
Milestone

Comments

@aaowens
Copy link

aaowens commented Feb 8, 2020

From Slack, I was looking for a good way to replicate something like

inds = (alldata.year .<= 1993) .& (alldata.ishead .== true)
alldata.labor_inc_spouse[inds] .= alldata.labor_inc_pre_spouse[inds]

in one line. In Stata this would be replace labor_inc_spouse = labor_inc_pre_spouse if year <= 1993 & ishead == true

@bkamins suggested

alldata.labor_inc_spouse .= ifelse.((alldata.year .<= 1993) .& (alldata.ishead .== true), alldata.labor_inc_pre_spouse, alldata.labor_inc_spouse)

which operates in place and is efficient.

It could be nice to have a macro version of that like

@replace(df, :labor_inc_spouse = :labor_inc_pre_spouse if :year .<= 1993 & :ishead .== true)
@pdeffebach
Copy link
Collaborator

I'm sorry I missed this issue when it came up over a year ago.

This is definitely something that needs serious consideration both here and at DataFramesMeta. here is discussion in DataFrames about the same issue.

I think a @ByRow macro will help with this, but making it work with missings will be tough.

Marking this as 1.X since its a new feature that can be added without breaking.

@pdeffebach pdeffebach added this to the 1.X milestone Mar 7, 2021
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

2 participants