R data.table roll =“最近".实际上不是最近的 [英] R data.table roll="nearest" not actually nearest

查看:68
本文介绍了R data.table roll =“最近".实际上不是最近的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given the following data.tables I'm surprised to see the 5.9 index matching with 5 rather than 6.

我不太了解发生了什么事.

I don't quite understand what's going on.

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行匹配.

I would expect to see 3 rows with the index==6 row in reference being matched with the index==5.9 row in dat, at least for my understanding on nearest.

这是预期的行为吗?

使用R 3.3.2,data.table 1.10.4

Using R 3.3.2, data.table 1.10.4

推荐答案

因为 1:10 是整数的向量,所以对整数和 as.integer(5.9)的联接完成了是5.

Because 1:10 is a vector of integers, the join in done on integers and as.integer(5.9) is 5.

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

You can, use 1:10+0 to build a numeric:

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

这篇关于R data.table roll =“最近".实际上不是最近的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆