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
I thought I've seen this FR before, but couldn't find it.
Would be nice if we could specify column names using regex expressions in .SDcols. Currently one has to do something like .SDcols = grep("mypattern", names(myDT)), which you can't chain on, and is pretty fragile.
Perhaps the patterns function from melt can be reused here, making the syntax .SDcols = patterns("mypattern").
I'd like to add, that patterns would be super useful in j as well.
It is often needed to select columns with grep and the only way is to refer it via names(), e.g. veryLongDataTableName[, grep('lag', names(veryLongDataTableName), with = FALSE]
or to remove multiple columns, e.g. dt[, (grep('lag', names(dt)) := NULL]
or to make new column names: dt[, paste0(grep('^test', names(dt), value = TRUE), '_sqrt') := lapply(.SD, sqrt), .SDcols = grep('^test', names(dt), value = TRUE)]
These would be much shorter with patterns available in j as well: veryLongDataTableName[, patterns('lag'), with = FALSE] dt[, patterns('lag') := NULL] dt[, paste0(patterns('^test'), '_sqrt') := lapply(.SD, sqrt), .SDcols = patterns('^test')]
Alternatively, it would be handy to have a special symbol for column names, selected in .SDcols, e.g. .NM
I'd like to add, that patterns would be super useful in j as well.
It is often needed to select columns with grep and the only way is to
refer it via names(), e.g.
veryLongDataTableName[, grep('lag', names(veryLongDataTableName), with =
FALSE]
or to remove multiple columns, e.g.
dt[, (grep('lag', names(dt)) := NULL]
or to make new column names:
dt[, paste0(grep('^test', names(dt), value = TRUE), '_sqrt') :=
lapply(.SD, sqrt), .SDcols = grep('^test', names(dt), value = TRUE)]
These would be much shorter with patterns available in j as well:
veryLongDataTableName[, patterns('lag'), with = FALSE]
dt[, patterns('lag') := NULL]
dt[, paste0(patterns('^test'), '_sqrt') := lapply(.SD, sqrt), .SDcols =
patterns('^test')]
Alternatively, it would be handy to have a special symbol for column
names, selected in .SDcols, e.g. .NM
Activity
ksavin commentedon Oct 26, 2016
I'd like to add, that
patternswould be super useful injas well.It is often needed to select columns with
grepand the only way is to refer it vianames(), e.g.veryLongDataTableName[, grep('lag', names(veryLongDataTableName), with = FALSE]or to remove multiple columns, e.g.
dt[, (grep('lag', names(dt)) := NULL]or to make new column names:
dt[, paste0(grep('^test', names(dt), value = TRUE), '_sqrt') := lapply(.SD, sqrt), .SDcols = grep('^test', names(dt), value = TRUE)]These would be much shorter with
patternsavailable in j as well:veryLongDataTableName[, patterns('lag'), with = FALSE]dt[, patterns('lag') := NULL]dt[, paste0(patterns('^test'), '_sqrt') := lapply(.SD, sqrt), .SDcols = patterns('^test')]Alternatively, it would be handy to have a special symbol for column names, selected in
.SDcols, e.g..NMMichaelChirico commentedon Oct 26, 2016
names(.SD) should suffice...
On Oct 26, 2016 12:37 PM, "ksavin" [email protected] wrote:
ksavin commentedon Oct 26, 2016
Forgot I am in fact using
names(.SD)for these cases :)Still, way cleaner and shorter with patterns.
MichaelChirico commentedon Oct 26, 2016
I do regularly things like
grep(pattern, names(.SD))(and maybe addvalue = TRUE)... Maybe I'm just used to this setup.mbacou commentedon Dec 8, 2016
Another one I tend to use is
.SDcols=names(.SD) %like% "mypattern", a little ugly. Upvoting this FR as well.hannes101 commentedon Mar 24, 2017
Just as a reference to a SO question, please update it there also if it got implemented :-)
https://stackoverflow.com/questions/42999949/select-data-table-columns-with-grep-like-partial-matching
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
A to update: https://stackoverflow.com/a/51331981/
HughParsonage commentedon May 18, 2018
I wrote
select_grepin package hutils before realizing this was an outstanding issue:Created on 2018-05-19 by the reprex package (v0.2.0).
Closes #1878 and #3185 -- .SDcols accepts patterns