计算每个唯一值的出现次数 [英] Count number of occurences for each unique value
本文介绍了计算每个唯一值的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
比方说我有
v = rep(c(1,2, 2, 2), 25)
现在,我想计算每个唯一值出现的次数。 unique(v)
返回唯一值是多少,但不返回它们的个数。
Now, I want to count the number of times each unique value appears. unique(v)
returns what the unique values are, but not how many they are.
> unique(v)
[1] 1 2
我想要可以给我的东西
length(v[v==1])
[1] 25
length(v[v==2])
[1] 75
,但更一般的是: )接近(但不太完全)的东西:
but as a more general one-liner :) Something close (but not quite) like this:
#<doesn't work right> length(v[v==unique(v)])
推荐答案
也许表是你要的?
dummyData = rep(c(1,2, 2, 2), 25)
table(dummyData)
# dummyData
# 1 2
# 25 75
## or another presentation of the same data
as.data.frame(table(dummyData))
# dummyData Freq
# 1 1 25
# 2 2 75
这篇关于计算每个唯一值的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文