DataFramesMeta.jl icon indicating copy to clipboard operation
DataFramesMeta.jl copied to clipboard

@replace for in-place conditional updating

Open aaowens opened this issue 6 years ago • 1 comments

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)

aaowens avatar Feb 08 '20 21:02 aaowens

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 avatar Mar 07 '21 19:03 pdeffebach