删除矩阵中的行

时间:2016-02-15 14:07:47

标签: r matrix

我有一个矩阵,看起来像这样:

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

2 个答案:

答案 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

<强>解释

  1. 首先,您通过apply
  2. 获取每行的字符串表示形式
  3. 然后计算每个标记在整个矩阵中的显示频率(通过rle
  4. 然后,您只选择那些出现次数超过2次的行