R data.table roll = "closest" not closest

Given the following data.tables, I am surprised to see that index 5.9 corresponds to 5, not 6.

I do not quite understand what is happening.

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"] 

I would expect to see 3 lines with index == 6 lines in the link matched with index == 5.9 lines in dat, at least for my understanding in the near.

Is this the expected behavior?

Using R 3.3.2, data.table 1.10.4

+5
source share
1 answer

Since 1:10 is a vector of integers, the union in integers and as.integer(5.9) is 5.

You can use 1:10+0 to create a number:

 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 
+6
source

Source: https://habr.com/ru/post/1268196/


All Articles