.shallow can retain keys with the argument retain.key = TRUE. However, this feature suffers from two problems that need to be solved before the function can be exported as requested in #2323:
Key gets lost if more columns than key are chosen with the cols argument.
library(data.table)
DT <- data.table(x1 = sample(1:10, 100, replace = TRUE),
x2 = sample(1:10, 100, replace = TRUE),
x3 = sample(1:10, 100, replace = TRUE),
y = rnorm(100),
key = c("x1", "x2", "x3"))
## as expected
identical(key(data.table:::.shallow(DT, retain.key = TRUE)), c("x1", "x2", "x3"))
# TRUE
identical(key(data.table:::.shallow(DT, cols = c("x1", "x2", "x3"), retain.key = TRUE)), c("x1", "x2", "x3"))
# TRUE
## key gets lost for no reason
test <- data.table:::.shallow(DT, cols = c("x1", "x2", "x3", "y"), retain.key = TRUE)
identical(key(test), c("x1", "x2", "x3"))
# FALSE
## although the copy is of course sorted properly
data.table:::is.sorted(test, by = c("x1", "x2", "x3"))
# TRUE
Indices remain, even if the columns are dropped
setindex(DT, x2, y)
setindex(DT, x1, y)
## index just gets copied, no matter if the columns are not present anymore
test <- data.table:::.shallow(DT, cols = c("x3"), retain.key = TRUE)
names(attributes(attr(test, "index")))
# "__x2__y" "__x1__y"
names(test)
# "x3"
The text was updated successfully, but these errors were encountered:
.shallow can retain keys with the argument
retain.key = TRUE
. However, this feature suffers from two problems that need to be solved before the function can be exported as requested in #2323:cols
argument.The text was updated successfully, but these errors were encountered: