-
Notifications
You must be signed in to change notification settings - Fork 991
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
Should be able to refer to i's .SD during a join. #935
Comments
That's be nice. |
Adding my hat to the ring for this, as it would help solve a problem I encountered recently, basically what @matthieugomez mentioned-- Namely, updating many columns by reference during a join operation, especially when the inner table may have hard-to-predict column names. That is,
At least with Here's an example:
It's easy to merge, but not to update |
Any news on that FR? Since the i.col and x.col syntax is now supported in joins since 1.9.7 this may be a good code base for implementing x.SD and i.SD variables? Allow x's cols to be referred to using 'x.' prefix I'd love to see this feature in the data.table! |
There is a related question on SO: How can I access all columns of |
A bit strange that This answer is very clunky; revisit when this is closed |
I spotted it is already possible to refer to d1 = data.table(id=1:3)
d2 = data.table(id=2:4, v1=5:7, v2=6:8)
d1[d2, .iSD, on="id", by=.EACHI]
#Empty data.table (0 rows and 3 cols): id,v1,v2
d1[d2, .SD, on="id", by=.EACHI]
#Empty data.table (0 rows and 3 cols): id,v1,v2 |
Adding to Jan's observation, we can trick library(data.table)
set.seed(45L)
dt1= data.table(x=rep(1:2, each=10), y=sample(8,20,TRUE), key="x")
dt2 = data.table(x=1:2, y1=1:2, y2=3:4, y3=5:6, y4=7:8, key="x")
dt1[dt2, .(sum(y>=y1), sum(y>=y2), sum(y>=y3), sum(y>=y4)), by=.EACHI]
#> x V1 V2 V3 V4
#> 1: 1 10 9 5 3
#> 2: 2 9 9 4 1
dt1[dt2, {quote(mget)
lapply(.iSD, function(x) sum(y >= x))}, by=.EACHI]
#> x x y1 y2 y3 y4
#> 1: 1 10 10 9 5 3
#> 2: 2 9 9 9 4 1 Here, I also have the |
For the sake of completeness, we can get the same result as in ColeMiler1's comment with
or
BTW, we can get rid of the redundant second
|
@UweBlock that seems more logical 😀 I assume |
Now if I'd like to join and for each match get the count of values
y >= y_i
. Usingby=.EACHI
this is quite simple:Having a
i.SD
would allow usage of:The text was updated successfully, but these errors were encountered: