jf328 jf328 - 3 months ago 28
R Question

R data.table join with roll

> dd = data.table(a = c(1,1), b = c(1,2), v = c(1, NA))
> dd
a b v
1: 1 1 1
2: 1 2 NA
> setkey(dd, a,b)
> dd[.(1,2), roll = TRUE, rollends = c(TRUE, TRUE)]
a b v
1: 1 2 NA


What have I missed here? Why isn't
v
carried forward?

Answer

Rolling join doesn't need to do rolling here as you are matching exact row (1, 2). Rolling matching is made when there is no match on actual values, in your case it has exact match. See below example which, I modified dd so there is no match on .(1,2).

library(data.table)
dd = data.table(a = c(1,1), b = c(1,3), v = c(1, NA))
dd[.(1,2), roll = TRUE, rollends = c(TRUE, TRUE)]
#   a b v
#1: 1 2 1
Comments