如何从矢量中删除超过计数的项目?

时间:2013-10-08 18:33:53

标签: r statistics

道歉,如果我不正确地问这个,但我是R的新手。说我有一个像这样的矢量X:

Sam
George
George
Suzzy
Suzzy
Suzzy
Leo

我想要的是能够获得包含仅出现两次的名称的向量,但保留它们在原始向量中出现的次数。以下是伪代码的外观示例:

x <- x[sum x.uniquecount() >=2]

最终结果:

George
George
Suzzy
Suzzy
Suzzy

2 个答案:

答案 0 :(得分:2)

 vec <- scan(what="")
1: Sam
2: George
3: George
4: Suzzy
5: Suzzy
6: Suzzy
7: Leo
8: 
Read 7 items


vec[ vec %in% names(table(vec))[table(vec) >= 2] ]
[1] "George" "George" "Suzzy"  "Suzzy"  "Suzzy" 

答案 1 :(得分:1)

df <- read.table(text = "Sam
George
George
Suzzy
Suzzy
Suzzy
Leo")

n <- ave(as.numeric(df$V1), df$V1, FUN = length)

df[n > 1, ]

# [1] George George Suzzy  Suzzy  Suzzy