0

Is there better way to compute this instead of:

length(unique(vector))

let's assume that we donno what class is the vector.

wBacz
  • 99
  • 9
  • @Cath, they are not asking to do this by group as far as I can see – talat Mar 28 '17 at 09:28
  • 1
    @docendodiscimus indeed but you can consider you have only one group and considering the target Q and the answers, I found it close enough (but looking for Q&A to add to the target) – Cath Mar 28 '17 at 09:30

1 Answers1

0

You can use

library(data.table)
vector <- c(1,2,2,2,3)
uniqueN(vector)
67342343
  • 816
  • 5
  • 11
  • I have found uniqueN to be 3 times slower: data(iris) x <- rep(unlist(iris),100000) start <- Sys.time() length(unique(x)) stop <- Sys.time() stop - start start <- Sys.time() uniqueN(x) stop <- Sys.time() stop - start – wBacz Mar 28 '17 at 09:56