我正在尝试使用data.tables在R中进行vlookup。我正在查找特定日期的值,如果该值不可用,我想要最近的下一个值。
table1 <- fread(
"id | date_created
1 | 2018-01-02
1 | 2018-01-03
2 | 2018-01-08
2 | 2018-01-09",
sep ="|"
)
table2<- fread(
"otherid | date | value
1 | 2018-01-02 | 1
2 | 2018-01-04 | 5
3 | 2018-01-07 | 3
4 | 2018-01-08 | 5
5 | 2018-01-11 | 3
6 | 2018-01-12 | 2",
sep = "|"
)
结果应如下所示:
table1 <- fread(
"id | date | value2
1 | 2018-01-02 | 1
1 | 2018-01-03 | 5
2 | 2018-01-08 | 5
2 | 2018-01-09 | 3",
sep ="|"
)
修改 我修好了,这行得通:
table1[, value2:= table2[table1, value, on = .(date=date_created), roll = -7]]
答案 0 :(得分:1)
table1[, value2:= table2[table1, value, on = .(date=date_created), roll = -7]]