Skip to content

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

@franknarf1

Description

@franknarf1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    joinsUse label:"non-equi joins" for rolling, overlapping, and non-equi joins

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions