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

With by=.EACHI and unmatched i, can we set nomatch= to get .SD[0] instead of .SD[NA]? #3452

Open
franknarf1 opened this issue Mar 11, 2019 · 0 comments
Labels
joins Use label:"non-equi joins" for rolling, overlapping, and non-equi joins

Comments

@franknarf1
Copy link
Contributor

franknarf1 commented Mar 11, 2019

Example from https://stackoverflow.com/q/25956178

library(data.table)
DT = data.table(
    x = c("A", "A", "A", "B", "B", "B"),
    y = c("i", "ii", "ii", "i", "i", "i"),
    z = 1:6
)

# .N correctly displays 0 for unmatched group
DT[CJ(x, y, unique=TRUE), on=.(x, y), .N, by=.EACHI]
#    x  y N
# 1: A  i 1
# 2: A ii 2
# 3: B  i 3
# 4: B ii 0

# .I correctly (?) displays 0 for unmatched group
DT[CJ(x, y, unique=TRUE), on=.(x, y), .I, by=.EACHI]
#    x  y I
# 1: A  i 1
# 2: A ii 2
# 3: A ii 3
# 4: B  i 4
# 5: B  i 5
# 6: B  i 6
# 7: B ii 0

# z incorrectly is NA for unmatched group
DT[CJ(x, y, unique=TRUE), on=.(x, y), sum(z), by=.EACHI]
#    x  y V1
# 1: A  i  1
# 2: A ii  5
# 3: B  i 15
# 4: B ii NA

Intuitively, I expect the latter to be 0, with sum(z) operating on .SD[0]$z for the unmatched group. Re #857, maybe this can also be included as an option (passing .SD[0] to j when .N == 0L), eg like nomatch = integer(0), though that's kind of esoteric notation.


More examples, I think: https://stackoverflow.com/q/57526710

@jangorecki jangorecki added the joins Use label:"non-equi joins" for rolling, overlapping, and non-equi joins label Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
joins Use label:"non-equi joins" for rolling, overlapping, and non-equi joins
Projects
None yet
Development

No branches or pull requests

2 participants