0

Can somebody give me an example where I can code this SQL in R by using dplyr or another package?

select *
from Data
inner join Data2
on Data2.x between Data2.y - 3 and Data2.y + 3
T. Beige
  • 177
  • 12
  • 1
    You can do a cross join by using `full_join` with `by=character()` and then `filter` it down but it is very inefficient. Best to use some other package such as sqldf or data.table. – G. Grothendieck Mar 16 '22 at 14:34
  • If you had some example data frames and desired output it'd be easier because I'm not entirely sure what your query returns, but have you tried something like this: `df1 %>% inner_join( df2 %>% filter(x >= y - 3 && x <= y + 3) )` – danh Mar 16 '22 at 14:51
  • See the dupe-links. Long-story-short, `dplyr` does not support join on ranges/overlaps, but `data.table`, `sqldf`, and `fuzzyjoin` all do, and the latter works fluidly in a `dplyr`-pipe. – r2evans Mar 16 '22 at 16:34
  • (Let me know if I misunderstood your question, but I think the dupe-links should resolve your needs.) – r2evans Mar 16 '22 at 16:35
  • Thanks to G.Grothendieck. sqldf is a good solution: https://stackoverflow.com/questions/70584557/mutate-a-variable-with-a-few-conditions – T. Beige Mar 17 '22 at 11:49

0 Answers0