Is there a data.table
idiomatic way to obtain the unique keys of a data table, when it is given as a single column?
I am working over a number of data sets, each of around 10 million rows and want to keep function calls/overhead to a minimum. With the below toy example,
require(data.table)
d_test<-data.table(id=c(1,1,2,7,2,3,5),
amt=c(100,200,400,600,231,-100,-200),
pay=c(-2,rep(1:3,2)),
key="id")
the output I am seeking is equivalent to, either as a vector or data.table,
unique(d_test[,.(id)])
, or unique(d_test$id)
that is, c(1,2,3,5,7)
unique(d_test[, key(d_test), with=FALSE])
, but for the specific case of a single-column key, your approaches and George's (in the answer below) seem fine. Note that the default ofunique.data.table
is to goby=key(x)
. See?unique.data.table
– Frank