Cut out scope between key and data.frame for data.table
When using data.table for searching, it is very fast. There is one behavior that does not work with my current workflow, and I am sure there is a better way and I am missing it. The behavior is to change in place, even if the key was taken from the parent data.frame
, data.table will act on the parent in data.frame
ways that may not always be desired.
Here's an example, as I am missing the language to express it correctly:
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
Note that the order is key
affected by usage data.table
even though it was not used in the search?
I know I data.table
avoid making copies, but is there a way to cut this link to key
and force the data.table
act on subkey
without changing key
?
source to share