You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This would allow the following type of code: DT[, names(.SD) := lapply(.SD, rev), .SDcols = -c(1,3,8)]
To reverse every column except 1, 3, and 8 in DT by reference. See discussion in #787. Maybe potentially even: DT[, := lapply(.SD, rev), .SDcols = -c(1,3,8)]
and have the names of the columns to be updated inferred from the names of the return value to lapply.
That would be very nice.
Another implementation would be that, when .SDcols is not specified, and .SD is present in j, the LHS of := is understood as .SDcols.
equivalently, though this is a good reminder that with can help in situations when wanting to use data.table programatically, which is another issue I've been discussing with Arun.
Also, note both fail with:
Error in (function (...) :
Check that is.data.table(DT) == TRUE. Otherwise, := and :=(...) are defined for use in j, once only and in particular ways. See help(":=").
It's unfortunate since the result of lapply is already named, so this is a shorthand for the \`:=\`(V1 = .POSIXct(V1, tz = 'UTC'), ...) approach of explicitly naming columns
I actually lean towards allowing .SD on the LHS of :=. More concise and I think the intent is clear. We're doing this with NSE so we can just capture .SD --> names(.SD) anyway.
I've been thinking about this FR again after having to do quite a bit of "manual" LHS creation in a current project. (FWIW my own preferred option is @MichaelChirico's DT[, .SD := ....], but would support any of the proposed solutions.)
Another possible syntax variant — which would involve even less typing if it is feasible to code up — would be to enable := directly in .SDcols. I'm not sure how others would feel about this, though.
It does ~basically read well here, but I would be against that... := semantics are (based on legion user reports/SO Q&A) confusing enough without opening up another API surface for it. Being able to consistently look only for j to know if a table is being updated by reference will keep code more readable than if := could show up in other [ arguments, possibly on other lines, possible separated from j by dozens of lines.
Activity
matthieugomez commentedon Nov 4, 2014
That would be very nice.
Another implementation would be that, when
.SDcolsis not specified, and .SD is present inj, the LHS of:=is understood as.SDcols.brodieG commentedon Nov 7, 2014
Alternate is to create a
.SDcolsobject that is usable in LHS of:=.Also, using
.SDin LHS of:=should probably throw an error instead of kind of work. More discussion on SO.jangorecki commentedon Nov 8, 2014
the straight "workaround" for this would be to use
with=FALSEexample in: http://jangorecki.github.io/blog/2014-11-07/Data-Anonymization-in-R.html#minimal-script
still the
.SDcolsobject usable inj(bothLHSandRHS) seems to be the best idea.brodieG commentedon Nov 8, 2014
Jan, not sure
with=Fis necessary, since you can do:or
equivalently, though this is a good reminder that
withcan help in situations when wanting to usedata.tableprogramatically, which is another issue I've been discussing with Arun.Also, note both fail with:
stefanfritsch commentedon Mar 3, 2015
Another inconsistency with this currently is that
this works:
and this doesn't:
The second fails with:
That precludes some elegant .SDcols syntax.
franknarf1 commentedon Nov 28, 2017
SO q to update: https://stackoverflow.com/questions/47535845/perform-operations-on-data-table-columns-based-on-regex?noredirect=1
MichaelChirico commentedon Oct 28, 2018
Unfortunate that this workaround is blocked by
:=:It's unfortunate since the result of
lapplyis already named, so this is a shorthand for the\`:=\`(V1 = .POSIXct(V1, tz = 'UTC'), ...)approach of explicitly naming columnsMichaelChirico commentedon Sep 24, 2019
I actually lean towards allowing
.SDon the LHS of:=. More concise and I think the intent is clear. We're doing this with NSE so we can just capture.SD-->names(.SD)anyway.That & whatever comes out of #3795 would make adding/editing many columns much less clunky
jangorecki commentedon Sep 24, 2019
or eventually which is more like
names(.SD)grantmcdermott commentedon Nov 10, 2021
I've been thinking about this FR again after having to do quite a bit of "manual" LHS creation in a current project. (FWIW my own preferred option is @MichaelChirico's
DT[, .SD := ....], but would support any of the proposed solutions.)Another possible syntax variant — which would involve even less typing if it is feasible to code up — would be to enable
:=directly in .SDcols. I'm not sure how others would feel about this, though.MichaelChirico commentedon Nov 11, 2021
It does ~basically read well here, but I would be against that...
:=semantics are (based on legion user reports/SO Q&A) confusing enough without opening up another API surface for it. Being able to consistently look only forjto know if a table is being updated by reference will keep code more readable than if:=could show up in other[arguments, possibly on other lines, possible separated fromjby dozens of lines.