我有一个共现类型数据的方阵,如:
m <- matrix(c(30, 30, 30, 30, 20, 0, 0,
30, 373, 30, 204, 207, 0, 290,
30, 30, 65, 65, 20, 35, 0,
30, 204, 65, 239, 38, 35, 156,
20, 207, 20, 38, 207, 0, 134,
0, 0, 35, 35, 0, 35, 0,
0, 290, 0, 156, 134, 0, 290),
nrow=7, byrow=TRUE)
通过比较上三角+对角线元素,有一些非对角线等于对角线。我想通过满足:
来删除行和列if ((m[i,j] == m[i,i]) & (m[i,j] < m[j,j]))
因此,只留下具有较大出现次数的行/列,并在元素始终与另一个元素共存时取出行/列。
输出应为:
373 204
204 239
谢谢!
答案 0 :(得分:2)
这是一种矢量化方法:
i <- as.vector(row(m))
j <- as.vector(col(m))
k <- matrix(m == m[cbind(i, i)] & m < m[cbind(j, j)], nrow(m))
# [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,] FALSE TRUE TRUE TRUE FALSE FALSE FALSE
# [2,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [3,] FALSE FALSE FALSE TRUE FALSE FALSE FALSE
# [4,] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# [5,] FALSE TRUE FALSE FALSE FALSE FALSE FALSE
# [6,] FALSE FALSE TRUE TRUE FALSE FALSE FALSE
# [7,] FALSE TRUE FALSE FALSE FALSE FALSE FALSE
delete.idx <- sort(unique(i[k]))
# [1] 1 3 5 6 7
keep.idx <- setdiff(seq_len(nrow(m)), delete.idx)
# [1] 2 4
m[keep.idx, keep.idx]
# [,1] [,2]
# [1,] 373 204
# [2,] 204 239