如何在R中使用列的特定值创建矩阵的子集?(如何根据矩阵某列的值创建R语言中的矩阵子集)
子集的创建方式多种多样,可以说子集的创建方式是无限的,因为它取决于更大或更小分析的目标。其中一种方式是基于矩阵某列的特定值创建矩阵的子集。在R中,我们可以轻松地使用subset函数完成此操作,如下例所示。
示例
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
广告