计算向量中不同值的数量 [英] Count number of distinct values in a vector
问题描述
我想得到一个标量值向量:有多少个不同的值。
I have a vector of scalar values of which I'm trying to get: "How many different values there are".
例如 group<-c(1,2,3,1,2,3,4,6)
的唯一值是 1,2,3,4,6
,所以我想得到 5
。
For instance in group <- c(1,2,3,1,2,3,4,6)
unique values are 1,2,3,4,6
so I want to get 5
.
我想出了:
length(unique(group))
但是我不确定这是最有效的方法。
But I'm not sure it's the most efficient way to do it. Isn't there a better way to do this?
注意:我的情况比示例更复杂,由大约1000个数字组成最多25个不同的值。
Note: My case is more complex than the example, consisting of around 1000 numbers with at most 25 different values.
推荐答案
以下是一些想法,指向您解决方案的所有观点已经非常快。 length(unique(x))
也是我会用的:
Here are a few ideas, all points towards your solution already being very fast. length(unique(x))
is what I would have used as well:
x <- sample.int(25, 1000, TRUE)
library(microbenchmark)
microbenchmark(length(unique(x)),
nlevels(factor(x)),
length(table(x)),
sum(!duplicated(x)))
# Unit: microseconds
# expr min lq median uq max neval
# length(unique(x)) 24.810 25.9005 27.1350 28.8605 48.854 100
# nlevels(factor(x)) 367.646 371.6185 380.2025 411.8625 1347.343 100
# length(table(x)) 505.035 511.3080 530.9490 575.0880 1685.454 100
# sum(!duplicated(x)) 24.030 25.7955 27.4275 30.0295 70.446 100
这篇关于计算向量中不同值的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!