Skip to content

Add %fin% set operator #2279

Description

@franknarf1

I saw this on SO:

library(data.table)
x = data.table(v = LETTERS[1:3])
y = data.table(v = c("A","D","E","F"))

The goal is to see if each row of x appears in y. I think it would be nice to have a function like...

`%fin%` = function(x, y) y[x, on=names(y), .N, by=.EACHI]$N > 0L

x[, found := .SD %fin% y ]
#    v found
# 1: A  TRUE
# 2: B FALSE
# 3: C FALSE

... to complement the other set operators in #547 .

Edit: I forgot this was here. It comes up pretty often on SO, eg https://stackoverflow.com/a/52525333

df.have[year == 2014 & grade == 9, rs := 
  df.have[replace(copy(.SD), c("year", "grade"), list(2013, 8)), on=.(f.name, year, grade), .N, by=.EACHI]$N > 0L
]

# should be
df.have[year == 2014 & grade == 9, rs := 
  .(f.name, year = 2013, grade = 8) %fin% df.have
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestjoinsUse 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