我有一个矩阵,看起来像这样:
have=rbind(matrix(c(1,1,1,1,2,2,2,2,3,3,3,3),ncol=3),c(5,5,5),c(5,5,5),c(5,5,5),c(1,1,1),c(1,1,1))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
[4,] 1 2 3
[5,] 5 5 5
[6,] 5 5 5
[7,] 5 5 5
[8,] 1 1 1
[9,] 1 1 1
我想删除行,如果它们只显示1或2次。理想情况下,我希望它是用户特定的,因此如果该行仅出现N次或更少,则应删除它。因此,如果N = 2,那么它应该删除最后两行,因为它们只出现两次:
want=rbind(matrix(c(1,1,1,1,2,2,2,2,3,3,3,3),ncol=3),c(5,5,5),c(5,5,5),c(5,5,5))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
[4,] 1 2 3
[5,] 5 5 5
[6,] 5 5 5
[7,] 5 5 5
另一方面,如果N = 3,那么它应该返回:
want_2=rbind(matrix(c(1,1,1,1,2,2,2,2,3,3,3,3),ncol=3))
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
[4,] 1 2 3
答案 0 :(得分:5)
rowRemove <- function(x, n) {
oc <- apply(x, 1, paste, collapse = "\r")
x[table(oc)[oc] > n, ]
}
rowRemove(have, 3)
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 2 3
# [3,] 1 2 3
# [4,] 1 2 3
答案 1 :(得分:1)
我将如何做到这一点:
<强>代码强>
code <- apply(have, 1, paste, collapse = "\t")
rl <- rle(code)
have[code %in% rl$values[rl$lengths > 2], ]
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 2 3
# [3,] 1 2 3
# [4,] 1 2 3
# [5,] 5 5 5
# [6,] 5 5 5
# [7,] 5 5 5
<强>解释强>
apply
rle
)