【问题标题】:R data.table roll="nearest" not actually nearestR data.table roll="nearest" 实际上不是最近的
【发布时间】:2017-05-24 22:06:47
【问题描述】:

鉴于以下 data.tables,我很惊讶地看到 5.9 索引与 5 而不是 6 匹配。

我不太明白发生了什么。

dat <- data.table(index = c(4.3, 5.9, 1.2), datval = runif(3)+10, 
datstuff="test")
reference <- data.table(index = 1:10, refjunk = "junk", refval = runif(10))

dat[, dat_index := index]
reference[dat, roll="nearest", on="index"]

我希望看到 3 行,参考中的 index==6 行与 dat 中的 index==5.9 行匹配,至少对于我最近的理解而言。

这是预期的行为吗?

使用 R 3.3.2,data.table 1.10.4

【问题讨论】:

  • 索引为整数,向下转换时整数被截断。

标签: r data.table


【解决方案1】:

因为1:10 是整数向量,所以对整数和as.integer(5.9) 进行的连接是5。

您可以使用1:10+0 构建一个数字:

reference <- data.table(index = 1:10+0, ref_index=1:10, refjunk = "junk", refval = runif(10))
reference[dat, roll="nearest", on="index"]

   index ref_index refjunk     refval   datval datstuff dat_index
1:   4.3         4    junk 0.09868848 10.37403     test       4.3
2:   5.9         6    junk 0.60545607 10.86906     test       5.9
3:   1.2         1    junk 0.50005336 10.07994     test       1.2

【讨论】:

    猜你喜欢
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 1970-01-01
    • 2013-08-02
    • 2017-12-11
    相关资源
    最近更新 更多