gpt4 book ai didi

r - R 中的多次重复(2 次、3 次、...)

转载 作者:行者123 更新时间:2023-12-04 09:30:44 27 4
gpt4 key购买 nike

搜索了一段时间,我知道这个问题还没有得到解答。假设我有以下向量
v <- c("a", "b", "b", "c","c","c", "d", "d", "d", "d")
我如何找到重复次数超过 1 次的那些值

(应该是 "c","c","c", "d", "d", "d", "d")
和超过 2 个重复

(应该是 "d", "d", "d", "d" )

功能 duplicated(v)只返回有重复的值。

最佳答案

您可以生成一个 table()然后检查v的哪些元素是表的相关子集的一部分,例如

R> v <- c("a", "b", "b", "c","c","c", "d", "d", "d", "d")
R> tab <- table(v)
R> tab
v
a b c d
1 2 3 4
R> v[v %in% names(tab[tab > 2])]
[1] "c" "c" "c" "d" "d" "d" "d"
R> v[v %in% names(tab[tab > 3])]
[1] "d" "d" "d" "d"

关于r - R 中的多次重复(2 次、3 次、...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29973061/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com