Alex
Alex

Reputation: 15708

How to perform a inner roll join with data table?

Take for example

library(data.table)
library(dplyr)
df_test_1 <- 
    data.table(time = c(seq(20, 40, by = 5))) %>%
    mutate(values = -time) %>%
    setkey(time)

df_test_2 <-
    data.table(time = c(15, 20, 26, 28)) %>%
    mutate(orig_time = time) %>%
    setkey(time)

The roll join df_test_2[df_test_1, roll = -Inf] produces:

   time orig_time values
1:   20        20    -20
2:   25        26    -25
3:   30        NA    -30
4:   35        NA    -35
5:   40        NA    -40

The last three rows contain NAs in the orig_time column because there are no records in df_test_2 with time greater than or equal to 30.

I would like to produce the 'inner' version of the above join, that is, do not output rows where there is no time matched, e.g.:

   time orig_time values
1:   20        20    -20
2:   25        26    -25

Upvotes: 0

Views: 910

Answers (1)

Jacob F
Jacob F

Reputation: 366

To replicate an inner join you can use the nomatch argument.

df_test_2[df_test_1, roll = -Inf, nomatch=0]

See also:

rolling joins data.table in R

R - Data.Table Rolling Joins

Upvotes: 3

Related Questions