How to delete different rows and columns of a matrix using a single line code in R?

R ProgrammingServer Side ProgrammingProgramming

Deletion or addition of rows and columns in a matrix of any size is mostly done by using single square brackets and it is also the easiest way. To delete rows and columns, we just need to use the column index or row index and if we want to delete more than one of them then we can separate them by commas by inserting them inside c as c(-1,-2). If we want to delete more than one rows or columns in a sequence then a colon can be used.

Examples

 Live Demo

> M<-matrix(1:100,nrow=10)
> M

Output

   [,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
> M[-2:-3,-6:-7]

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 11 21 31 41 71 81 91
[2,] 4 14 24 34 44 74 84 94
[3,] 5 15 25 35 45 75 85 95
[4,] 6 16 26 36 46 76 86 96
[5,] 7 17 27 37 47 77 87 97
[6,] 8 18 28 38 48 78 88 98
[7,] 9 19 29 39 49 79 89 99
[8,] 10 20 30 40 50 80 90 100
> M[-1:-3,-6:-10]

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 4 14 24 34 44
[2,] 5 15 25 35 45
[3,] 6 16 26 36 46
[4,] 7 17 27 37 47
[5,] 8 18 28 38 48
[6,] 9 19 29 39 49
[7,] 10 20 30 40 50
> M[c(-1,-3),c(-6,-10)]

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 2 12 22 32 42 62 72 82
[2,] 4 14 24 34 44 64 74 84
[3,] 5 15 25 35 45 65 75 85
[4,] 6 16 26 36 46 66 76 86
[5,] 7 17 27 37 47 67 77 87
[6,] 8 18 28 38 48 68 78 88
[7,] 9 19 29 39 49 69 79 89
[8,] 10 20 30 40 50 70 80 90
> M[c(-5,-8),c(-2,-9)]

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 21 31 41 51 61 71 91
[2,] 2 22 32 42 52 62 72 92
[3,] 3 23 33 43 53 63 73 93
[4,] 4 24 34 44 54 64 74 94
[5,] 6 26 36 46 56 66 76 96
[6,] 7 27 37 47 57 67 77 97
[7,] 9 29 39 49 59 69 79 99
[8,] 10 30 40 50 60 70 80 100
> M[c(-5,-6:-8),c(-2,-9)]

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1 21 31 41 51 61 71 91
[2,] 2 22 32 42 52 62 72 92
[3,] 3 23 33 43 53 63 73 93
[4,] 4 24 34 44 54 64 74 94
[5,] 9 29 39 49 59 69 79 99
[6,] 10 30 40 50 60 70 80 100
> M[c(-5),c(-2,-5:-9)]

Output

   [,1] [,2] [,3] [,4]
[1,] 1 21 31 91
[2,] 2 22 32 92
[3,] 3 23 33 93
[4,] 4 24 34 94
[5,] 6 26 36 96
[6,] 7 27 37 97
[7,] 8 28 38 98
[8,] 9 29 39 99
[9,] 10 30 40 100
> M[c(-2,-5),c(-2:-4,-5,-9)]

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 1 51 61 71 91
[2,] 3 53 63 73 93
[3,] 4 54 64 74 94
[4,] 6 56 66 76 96
[5,] 7 57 67 77 97
[6,] 8 58 68 78 98
[7,] 9 59 69 79 99
[8,] 10 60 70 80 100
raja
Published on 04-Sep-2020 09:23:41
Advertisements