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

Add %fin% set operator #2279

Open
franknarf1 opened this issue Jul 31, 2017 · 0 comments
Open

Add %fin% set operator #2279

franknarf1 opened this issue Jul 31, 2017 · 0 comments

Comments

@franknarf1
Copy link
Contributor

franknarf1 commented Jul 31, 2017

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
]
@jangorecki jangorecki changed the title [Request:] Add %fin% set operator Add %fin% set operator Apr 6, 2020
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

2 participants