返回满足条件的最小行和最小列索引,并处理行关系

时间:2019-11-24 10:16:37

标签: r

我有

kappa2<- structure(c(2, 7, 14, 21, 30, 35, 46, 49, 58, 68, 76, 83, 91, 
96, 97, 98, 103, 113, 120, 125, 131, 135, 145, 152, 157, 164, 
171, 175, 183, 191, 5, 12, 19, 28, 33, 44, 47, 56, 66, 74, 81, 
89, 94, 95, 96, 101, 111, 118, 123, 129, 133, 143, 150, 155, 
162, 169, 173, 181, 189, 189, 7, 14, 23, 28, 39, 42, 51, 61, 
69, 76, 84, 89, 90, 91, 96, 106, 113, 118, 124, 128, 138, 145, 
150, 157, 164, 168, 176, 184, 184, 184, 7, 16, 21, 32, 35, 44, 
54, 62, 69, 77, 82, 83, 84, 89, 99, 106, 111, 117, 121, 131, 
138, 143, 150, 157, 161, 169, 177, 177, 177, 177, 9, 14, 25, 
28, 37, 47, 55, 62, 70, 75, 76, 77, 82, 92, 99, 104, 110, 114, 
124, 131, 136, 143, 150, 154, 162, 170, 170, 170, 170, 170, 5, 
16, 19, 28, 38, 46, 53, 61, 66, 67, 68, 73, 83, 90, 95, 101, 
105, 115, 122, 127, 134, 141, 145, 153, 161, 161, 161, 161, 161, 
161, 11, 14, 23, 33, 41, 48, 56, 61, 62, 63, 68, 78, 85, 90, 
96, 100, 110, 117, 122, 129, 136, 140, 148, 156, 156, 156, 156, 
156, 156, 156, 3, 12, 22, 30, 37, 45, 50, 51, 52, 57, 67, 74, 
79, 85, 89, 99, 106, 111, 118, 125, 129, 137, 145, 145, 145, 
145, 145, 145, 145, 145, 9, 19, 27, 34, 42, 47, 48, 49, 54, 64, 
71, 76, 82, 86, 96, 103, 108, 115, 122, 126, 134, 142, 142, 142, 
142, 142, 142, 142, 142, 142, 10, 18, 25, 33, 38, 39, 40, 45, 
55, 62, 67, 73, 77, 87, 94, 99, 106, 113, 117, 125, 133, 133, 
133, 133, 133, 133, 133, 133, 133, 133, 8, 15, 23, 28, 29, 30, 
35, 45, 52, 57, 63, 67, 77, 84, 89, 96, 103, 107, 115, 123, 123, 
123, 123, 123, 123, 123, 123, 123, 123, 123, 7, 15, 20, 21, 22, 
27, 37, 44, 49, 55, 59, 69, 76, 81, 88, 95, 99, 107, 115, 115, 
115, 115, 115, 115, 115, 115, 115, 115, 115, 115, 8, 13, 14, 
15, 20, 30, 37, 42, 48, 52, 62, 69, 74, 81, 88, 92, 100, 108, 
108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 108, 5, 
6, 7, 12, 22, 29, 34, 40, 44, 54, 61, 66, 73, 80, 84, 92, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
1, 2, 7, 17, 24, 29, 35, 39, 49, 56, 61, 68, 75, 79, 87, 95, 
95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 1, 6, 
16, 23, 28, 34, 38, 48, 55, 60, 67, 74, 78, 86, 94, 94, 94, 94, 
94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 94, 5, 15, 22, 27, 
33, 37, 47, 54, 59, 66, 73, 77, 85, 93, 93, 93, 93, 93, 93, 93, 
93, 93, 93, 93, 93, 93, 93, 93, 93, 93, 10, 17, 22, 28, 32, 42, 
49, 54, 61, 68, 72, 80, 88, 88, 88, 88, 88, 88, 88, 88, 88, 88, 
88, 88, 88, 88, 88, 88, 88, 88, 7, 12, 18, 22, 32, 39, 44, 51, 
58, 62, 70, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 78, 
78, 78, 78, 78, 78, 78, 5, 11, 15, 25, 32, 37, 44, 51, 55, 63, 
71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 71, 
71, 71, 71, 71, 6, 10, 20, 27, 32, 39, 46, 50, 58, 66, 66, 66, 
66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 
66, 66, 4, 14, 21, 26, 33, 40, 44, 52, 60, 60, 60, 60, 60, 60, 
60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 
10, 17, 22, 29, 36, 40, 48, 56, 56, 56, 56, 56, 56, 56, 56, 56, 
56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 56, 7, 12, 
19, 26, 30, 38, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 
46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 5, 12, 19, 23, 
31, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 
39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 7, 14, 18, 26, 34, 34, 
34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 
34, 34, 34, 34, 34, 34, 34, 34, 7, 11, 19, 27, 27, 27, 27, 27, 
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 
27, 27, 27, 27, 27, 27, 4, 12, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 
16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8), .Dim = c(30L, 30L), .Dimnames = list(
    c("kappa", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
    "", ""), NULL))

我希望kappa2的元素大于x的最小行和最小列索引

例如对于x = 80,它将是kappa2 [11,2],以及如何处理多次出现(行索引中的联系)。如果行之间有关系,我希望所有列索引都满足。

2 个答案:

答案 0 :(得分:1)

我们可以将whicharr.ind = TRUE一起使用,以获得大于x的矩阵的行和列索引。然后,我们可以根据行和列的索引order对其进行选择,并选择第一个值。

x = 80
mat <- which(kappa2 > x, arr.ind = TRUE)
mat[order(mat[, 1], mat[, 2]), ][1, ]

#row col 
# 11   2 

如果行中有平局,它将选择列中的最小值。


如果我们要选择最小行的所有列值,则可以

mat <- which(kappa2 > x, arr.ind = TRUE)
mat <- mat[order(mat[, 1], mat[, 2]), ]
mat[mat[, 1] == mat[1, 1], ]

# row col
#  11   2
#  11   3
#  11   4

答案 1 :(得分:1)

也许以下代码可以为您提供帮助

SUBSTR(x, -2)

显示所有行和列的最小值

which(kappa2*(kappa2>x)==min(kappa2[kappa2>x]),arr.ind = T)