Given a data.table
, how do I find the number of unique keys it contains?
library(data.table)
z <- data.table(id=c(1,2,1,3),key=\"id\")
length(uni
I'll expand my comment as an answer.
base::unique
(unique.default
) on vectors uses hash tables and is quite efficient, with average complexity of O(1) - this is very likely to be the general case. The worst case complexity is O(n). But the chances of that happening at each insert/search should be extremely rare - it must be a terrible hash function if it does.
In your question, you've only one key column, and therefore base's unique should be quite efficient. However, on more than one column, unique.data.frame
is very inefficient - as it coerces all the columns to characters, then pastes them together and then calls unique.default
on it.
You can use:
nrow(unique(z))
data.table's unique
method, by default, provides key columns to its by
argument. And since we know the data is already sorted, instead of ordering, we use data.table:::uniqlist
to fetch the indices corresponding to unique rows much more efficiently in O(n)
as well. It's therefore efficient on any amount of key columns.
However we could add this information as an attribute while setting the key, as it's quite straightforward.