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

colA:colB syntax should also work on LHS of LHS := RHS #1710

Closed
arunsrinivasan opened this issue May 17, 2016 · 3 comments
Closed

colA:colB syntax should also work on LHS of LHS := RHS #1710

arunsrinivasan opened this issue May 17, 2016 · 3 comments

Comments

@arunsrinivasan
Copy link
Member

require(data.table)
dt = data.table(x=c(2,2,2,1,1,1), a=1, b=2, c=3)

dt[, lapply(.SD, sum), by=x, .SDcols=a:c]
#    x a b c
#1: 2 3 6 9
#2: 1 3 6 9

This way of specifying start:end also works with by=. It'd be great to get this to work on LHS.

@eantonya
Copy link
Contributor

eantonya commented Jan 4, 2017

Related - also allow list/. syntax on LHS: dt[, .(a, b, c) := as.list(1:3)].

@franknarf1
Copy link
Contributor

franknarf1 commented Feb 17, 2017

Also, the !cols syntax, I think. Just ran into that here: http://stackoverflow.com/a/42301797/ where I can use .SDcols = !k but have to use setdiff(names(res), k) :=

I guess (.SDcols) := would work for that case, though, not sure where that issue is filed. Oh, here: #795 (comment)

New example: https://stackoverflow.com/questions/45446830/drop-in-place-all-but-specified-columns-by-name-from-data-table

@MichaelChirico
Copy link
Member

I think we can just use names(.SD) on the LHS now, seems easier than introducing more NSE complications in j:

dt[, names(.SD) := lapply(.SD, sum), by=x, .SDcols=a:c]
dt
#        x     a     b     c
#    <num> <num> <num> <num>
# 1:     2     3     6     9
# 2:     2     3     6     9
# 3:     2     3     6     9
# 4:     1     3     6     9
# 5:     1     3     6     9
# 6:     1     3     6     9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants