How to take a subset of a matrix in R with finite values only if the matrix contains NA and Inf values?

R ProgrammingServer Side ProgrammingProgramming

If we have a matrix that contains NA or Inf values and we want to take the subset of that matrix with finite values then only the rows that do not contain NA or Inf values will be the output. We can do this in R by using rowSums and is.finite function with negation operator !.

Example

 Live Demo

set.seed(999)
M1<-matrix(sample(c(1:10,NaN,Inf),25,replace=TRUE),ncol=5)
M1

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 5    2   4    10   NaN
[2,] 7    8    7    2   8
[3,] 2    1   1    10   7
[4,] NaN  5   Inf   2   5
[5,] 10   8   2    1   10

Example

Finite_M1<-M1[!rowSums(!is.finite(M1)),] Finite_M1

Output

    [,1] [,2] [,3] [,4] [,5]
[1,] 7    6    7    1    9
[2,] 9    3    6    5    5
[3,] 1    7    3    3    7

Example

 Live Demo

M2<-matrix(sample(c(0:25,NaN,Inf),64,replace=TRUE),ncol=8)
M2

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 7   13   Inf   3   3    3    8   Inf
[2,] 9   9   12   25   20    5    18   9
[3,] Inf 10   13  20   19    5    8    1
[4,] 0   9   22   17   24    2    1   15
[5,] 22  15   1   21   16   17    14   Inf
[6,] 12  9   Inf   10  NaN   19   22  20
[7,] 16  21   9   16  10    18    6   21
[8,] 21  NaN  16  18  25    11    25  20

Example

Finite_M2<-M2[!rowSums(!is.finite(M2)),]
Finite_M2

Output

   [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 4   22   23   1    24   16  19   7
[2,] 24   15  7    12    1   11  6   24
[3,] 25   10  25  24    12   18  22   18
[4,] 23   0 3 21     11    8  4   14

Example

 Live Demo

M3<-matrix(sample(c(0:10,NaN,Inf),36,replace=TRUE),ncol=6)
M3

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 5 0 8 6 2 7
[2,] 3 7 4 8 8 1
[3,] 1 0 2 6 9 1
[4,] 6 4 2 0 7 6
[5,] 8 3 2 3 6 0
[6,] 6 2 1 0 7 6

Example

Finite_M3<-M3[!rowSums(!is.finite(M3)),]
Finite_M3

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 7 5 4 7 9 8
[2,] 5 9 7 8 9 8

Example

 Live Demo

M4<-matrix(sample(c(51:100,NaN,Inf),100,replace=TRUE),ncol=10)
M4

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 66 71 69 81 64 63 90 55 70 92
[2,] 75 72 72 92 55 96 95 79 61 70
[3,] 84 76 89 73 84 86 85 83 72 83
[4,] 65 85 54 97 71 91 65 76 51 72
[5,] 76 66 61 61 71 58 Inf 85 73 84
[6,] 87 62 69 81 65 95 93 62 NaN 51
[7,] 83 68 81 86 89 80 Inf 93 92 63
[8,] 66 60 67 56 62 62 55 60 64 69
[9,] 60 83 80 70 88 72 69 Inf 96 55
[10,] 56 74 69 66 78 94 62 79 77 59

Example

Finite_M4<-M4[!rowSums(!is.finite(M4)),] Finite_M4

Output

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,] 93   85   56    76   60   100  79 92 85 57
 [2,] 100  100  59     93   60   86 100 64 98 60
 [3,] 88   75   98    77   75   56 82 52 69 67
[4,] 59    51   94   94   78   100 86 75 51 59
[5,] 100   90   65   62   82    54 99 63 70 76

Example

 Live Demo

M5<-matrix(sample(c(101:125,NaN,Inf),25,replace=TRUE),ncol=5)
M5

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 114 117 124 125 121
[2,] 117 114 111 116 107
[3,] 117 117 107 116 115
[4,] 122 114 108 121 113
[5,] 121 103 106 Inf 106

Example

Finite_M5<-M5[!rowSums(!is.finite(M5)),] Finite_M5

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 110 119 117 123 125
[2,] 102 120 117 112 111
[3,] 108 124 125 103 123

Example

 Live Demo

M6<-matrix(sample(c(1001:1009,NaN,Inf),36,replace=TRUE),ncol=6)
M6

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1002 1008 1005 1005 NaN 1008
[2,] 1003 1007 1007 1004 1009 1002
[3,] 1008 1003 1009 1006 Inf Inf
[4,] 1007 1005 1005 1004 1004 1004
[5,] NaN 1002 1004 1003 1001 1008
[6,] 1009 1006 1005 1007 1008 1007

Example

Finite_M6<-M6[!rowSums(!is.finite(M6)),] Finite_M6

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1009 1007 1002 1007 1002 1009
[2,] 1004 1003 1004 1006 1001 1006

Example

 Live Demo

M7<-matrix(sample(c(91:99,NaN,Inf),64,replace=TRUE),ncol=8)
M7

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]  91   95  98  96 99   NaN  94  96
[2,]  93  Inf  94  98 NaN NaN 97 Inf
[3,]  93  97  97  94 94   93 NaN  92
[4,]  96  92  Inf  92 93 98 94    98
[5,]  93  97  95  92 NaN 91 93  99
[6,]  95  96  91  99 97 99 97 97
[7,]  99  96  95  91 96 93 95 96
[8,]  93  98  91  92 91 Inf 96 92

Example

Finite_M7<-M7[!rowSums(!is.finite(M7)),] Finite_M7

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 95 99 98 98 92 93 97 92
[2,] 94 99 98 98 99 97 92 96
raja
Updated on 10-Oct-2020 12:50:52

Advertisements