计算R中的唯一值 [英] Count unique values in R

查看:515
本文介绍了计算R中的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

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

p>

I want something that gives me

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)])


推荐答案

p>也许表是你以后的?

Perhaps table is what you are after?

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

这篇关于计算R中的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆