0

抱歉,如果我问错了,但我是 R 新手。假设我有一个这样的向量 X:

Sam
George
George
Suzzy
Suzzy
Suzzy
Leo

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

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

最后的结果:

George
George
Suzzy
Suzzy
Suzzy
4

2 回答 2

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" 
于 2013-10-08T18:36:10.927 回答
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
于 2013-10-08T18:47:59.757 回答