When using data.table as a lookup it is very fast. There is one behavior that does not work with my current workflow and I'm sure there's a better way and I'm missing it. The behavior is that of modifying in place even if the key was taken from a parent data.frame, data.table will act on the parent data.frame in ways that may not always be desirable.
Here's an example since I lack the language to express it properly:
library(data.table)
set.seed(123)
N <- 100
key <- data.frame(x = sample.int(N, N), y = 1:N, z = 1:N)
key$w <- key$x
head(key)
## x y z w
## 1 29 1 1 29
## 2 79 2 2 79
## 3 41 3 3 41
## 4 86 4 4 86
## 5 91 5 5 91
## 6 5 6 6 5
set.seed(1)
terms <- data.frame(z = sample.int(2 * N, 1e2, replace = TRUE))
subkey <- key[c("x", "y")]
setDT(subkey)
setDT(terms)
setkey(subkey, x)
subkey[terms][[2]]
head(key)
## x y z w
## 1 1 74 1 1
## 2 2 35 2 2
## 3 3 51 3 3
## 4 4 18 4 4
## 5 5 6 5 5
## 6 6 54 6 6
Notice the order of key is affected by the use of data.table even though it wasn't used in the lookup?
I know data.table is avoiding making copies but is there a way to cut this link to key and force data.table to act on subkey without modifying key?
Rather than
subkey <- key[c("x", "y")]
setDT(subkey)
just do
subkey <- as.data.table(key[c("x", "y")])
That will force a copy and sever the connection
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With