简体   繁体   English

在 R 中计算向量中的重复项

[英]Count Duplicates In Vector In R

group=c(1,1,2,2,3,4,4,5,5,6)

I am wishing to generate an output that looks like:我希望生成一个如下所示的输出:

dup:4重复:4

nodup: 2点头:2

Basically I want to count how many values are duplicated 'dup' and count how many are not 'nodup'基本上我想计算有多少值是重复的“dup”并计算有多少不是“nodup”

Here is an option, get the frequency count of values with table , convert to logical > 1 and then do a table again这是一个选项,使用table获取值的频率计数,转换为逻辑> 1然后再次做一个table

table(table(group) > 1)

If we need the label names as 'dup', 'nodup'如果我们需要标签名称为 'dup', 'nodup'

table(c('nodup', 'dup')[1 + (table(group) > 1)])
#    dup nodup 
#    4     2 

Another base R solution using duplicated + unique另一个使用duplicated + unique基本 R 解决方案

dup <- sum(duplicated(group))
nodup <- length(unique(group))-dup

such that以至于

> dup
[1] 4
> nodup
[1] 2
library(dplyr)
data.frame(group) %>%
    count(group) %>%
    count(dup = n > 1)

Also a base R option:也是一个base R选项:

table(rle(sort(group))$lengths > 1)

FALSE  TRUE 
    2     4 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM