子集可以通过许多不同的方式来要求,我们可以说,子集可能有无数种方式,因为这取决于进行较大或较小分析的目的。一种这样的方式是基于矩阵的列的某个值来对矩阵进行子集化。在R中,我们可以借助子集函数轻松地执行以下操作,如下例所示。
M<-matrix(1:100,ncol=10) M
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,1]<6)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95
subset(M,M[,1]>6)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 7 17 27 37 47 57 67 77 87 97 [2,] 8 18 28 38 48 58 68 78 88 98 [3,] 9 19 29 39 49 59 69 79 89 99 [4,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,1]==6)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96
subset(M,M[,1]>3)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 4 14 24 34 44 54 64 74 84 94 [2,] 5 15 25 35 45 55 65 75 85 95 [3,] 6 16 26 36 46 56 66 76 86 96 [4,] 7 17 27 37 47 57 67 77 87 97 [5,] 8 18 28 38 48 58 68 78 88 98 [6,] 9 19 29 39 49 59 69 79 89 99 [7,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,1]<8)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97
subset(M,M[,2]<20)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99
subset(M,M[,2]<25)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,5]>45)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96 [2,] 7 17 27 37 47 57 67 77 87 97 [3,] 8 18 28 38 48 58 68 78 88 98 [4,] 9 19 29 39 49 59 69 79 89 99 [5,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,5]>48)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 9 19 29 39 49 59 69 79 89 99 [2,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,8]>75)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96 [2,] 7 17 27 37 47 57 67 77 87 97 [3,] 8 18 28 38 48 58 68 78 88 98 [4,] 9 19 29 39 49 59 69 79 89 99 [5,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,9]>81)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 2 12 22 32 42 52 62 72 82 92 [2,] 3 13 23 33 43 53 63 73 83 93 [3,] 4 14 24 34 44 54 64 74 84 94 [4,] 5 15 25 35 45 55 65 75 85 95 [5,] 6 16 26 36 46 56 66 76 86 96 [6,] 7 17 27 37 47 57 67 77 87 97 [7,] 8 18 28 38 48 58 68 78 88 98 [8,] 9 19 29 39 49 59 69 79 89 99 [9,] 10 20 30 40 50 60 70 80 90 100
subset(M,M[,9]<90)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99
subset(M,M[,10]<100)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99
subset(M,M[,10]<96)
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95