Suppose I have a vector and I don’t know, apriori, its unique elements (here: 1 and 2).
vec <-
c(1, 1, 1, 2, 2, 2, 2)
I was interested in knowing is there a better way (or elegant way) of getting the number of unique elements in vec i.e. the same result as table(vec). It doesn’t matter if its a data.frame or a named vector.
R> table(vec)
vec
1 2
3 4
Reason: I was curious to know if there is a better way. Also, I noticed that there is a for loop in the base implementation (in addition to .C call). I don’t know if it’s a big concern, but when I do something like
R> table(rep(1:1000,100000))
R takes really long time. I am sure it’s because of the huge number 100000. But is there a way of making it faster?
EDIT This also does a good job in addition to Chase's answer.
R> rle(sort(sampData))
This is an interesting problem – I’m curious to see other thoughts on this. Looking at the source for
table()reveals that it builds off oftabulate().tabulate()has a few quirks apparently, namely that it only deals with positive integers and returns an integer vector without names. We can useunique()on our vector to apply thenames(). If you need to tabulate zero or negative values, I guess going back and reviewingtable()would be necessary astabulate()doesn’t seem to do that per the examples on the help page.And a quick test:
EDIT: I just realized there is a
count()function inplyrwhich is another alternative totable(). In the test above, it performs better thantable(), and slightly worse than the hack-job solution I put together: