data.table roll join无法正常工作

时间:2015-08-18 23:17:15

标签: r data.table

我正在尝试连接两个data.table。这是一个例子:

library(data.table)

tmp1 <- data.table(structure(list(Code = c("AED", "AED", "AED", "AED", "AED"),
                                  Date = structure(c(97286400, 97372800, 97459200, 97545600, 97632000),
                                                   class = c("POSIXct", "POSIXt"), tzone = "UTC")),
                             .Names = c("Code", "Date"), row.names = c(NA, -5L), class = "data.frame"))

tmp2 <- data.table(structure(list(Date = structure(c(97286400, 99705600, 102297600), tzone = "UTC",
                                                   class = c("POSIXct", "POSIXt")),
                                  Val = c(4.39, 3.96, 3.9474), Code = c("AED", "AED", "AED")),
                             .Names = c("Date", "Val", "Code"), row.names = c(NA, -3L), class = "data.frame"))

> tmp1
   Code       Date
1:  AED 1973-01-31
2:  AED 1973-02-01
3:  AED 1973-02-02
4:  AED 1973-02-03
5:  AED 1973-02-04

> tmp2
         Date    Val Code
1: 1973-01-31 4.3900  AED
2: 1973-02-28 3.9600  AED
3: 1973-03-30 3.9474  AED

> setkey(tmp1,Code,Date)

> setkey(tmp2,Code,Date)

> tmp2[tmp1,roll=TRUE]
         Date  Val Code
1: 1973-01-31 4.39  AED
2: 1973-02-01 4.39  AED
3: 1973-02-02 4.39  AED
4: 1973-02-03 4.39  AED
5: 1973-02-04 4.39  AED

> tmp2[tmp1,roll=2]
         Date  Val Code
1: 1973-01-31 4.39  AED
2: 1973-02-01   NA  AED
3: 1973-02-02   NA  AED
4: 1973-02-03   NA  AED
5: 1973-02-04   NA  AED

第一次滚动正常工作。在第二个例子中,根据文档的说法,我希望将4.39结转到1973-02-02:“当滚动是一个正数时,这限制了值向前推进的程度。”我希望看到:

> tmp2[tmp1,roll=2]
         Date  Val Code
1: 1973-01-31 4.39  AED
2: 1973-02-01 4.39  AED
3: 1973-02-02 4.39  AED
4: 1973-02-03   NA  AED
5: 1973-02-04   NA  AED

这是一个错误还是我误解了这个功能?

1 个答案:

答案 0 :(得分:3)

你解释得很好。原因是您的日期为POSIXct,因此roll号码以秒为单位,而不是天数。将您的滚动设置为2天,以秒为单位:

class(tmp1$Date)
> class(tmp1$Date)
[1] "POSIXct" "POSIXt"
> tmp2[tmp1, roll=2*3600*24]
         Date  Val Code
1: 1973-01-31 4.39  AED
2: 1973-02-01 4.39  AED
3: 1973-02-02 4.39  AED
4: 1973-02-03   NA  AED
5: 1973-02-04   NA  AED

或者通过Date强制您的Date:=as.Date(Date)并使用roll=2,具体取决于您的偏好。