How to find the position of odd numbers in an R vector?

R ProgrammingServer Side ProgrammingProgramming

To find the position of odd numbers in an R vector, we can find the position of values that are divisible by 2 with the help of which function. For example, if we have a vector called x then we can find the position of odd numbers using the command which(x%%2==1). Check out the below examples to understand how it works.

Example

 Live Demo

x1<-rpois(200,10)
x1

Output

  [1] 13 10 10 11 7 12 12 7 10 7 13 11 10 13 12 5 10 5 9 8 11 7 15 5 7
 [26] 11 14 9 15 12 6 13 16 13 13 11 10 20 7 9 4 12 6 8 6 14 15 11 18 8
 [51] 9 9 11 10 9 9 11 10 7 8 13 13 13 11 4 9 11 14 6 11 11 10 10 11 12
 [76] 10 10 10 5 10 9 11 9 10 18 12 10 8 10 8 17 8 11 10 5 15 12 12 5 10
[101] 18 6 7 6 9 7 14 13 10 8 8 17 12 10 9 12 10 11 11 8 11 13 11 11 14
[126] 9 17 6 13 11 9 8 8 6 10 13 7 13 14 11 9 11 8 8 11 10 11 14 11 14
[151] 10 11 12 8 13 16 15 14 17 15 9 5 6 12 13 11 10 7 8 12 10 8 8 7 9
[176] 14 7 19 10 7 8 10 9 13 6 11 8 14 16 9 5 13 9 15 6 7 7 8 7 11

Example

which(x1%%2==1)

Output

[1] 1 4 5 8 10 11 12 14 16 18 19 21 22 23 24 25 26 28
[19] 29 32 34 35 36 39 40 47 48 51 52 53 55 56 57 59 61 62
[37] 63 64 66 67 70 71 74 79 81 82 83 91 93 95 96 99 103 105
[55] 106 108 112 115 118 119 121 122 123 124 126 127 129 130 131 136 137 138
[73] 140 141 142 145 147 149 152 155 157 159 160 161 162 165 166 168 174 175
[91] 177 178 180 183 184 186 190 191 192 193 194 196 197 199 200

Example

 Live Demo

x2<-rpois(200,5)
x2

Output

  [1] 8 5 3 7 10 7 6 6 6 7 5 9 7 4 6 2 4 2 4 9 6 6 6 2 3
 [26] 5 6 6 5 4 5 2 5 6 2 4 4 11 6 4 4 5 2 6 5 4 10 3 2 7
 [51] 8 4 3 3 5 3 3 3 6 4 4 5 6 4 6 3 6 4 2 4 7 8 2 7 1
 [76] 7 3 3 2 4 5 7 4 2 2 3 8 7 9 5 8 4 7 7 9 2 5 4 3 9
[101] 5 5 9 1 10 5 3 7 2 3 4 5 7 7 7 3 4 2 6 3 5 6 5 2 2
[126] 5 6 6 2 3 4 4 5 8 6 3 3 4 6 9 6 4 7 2 5 5 7 7 9 7
[151] 2 8 1 5 5 5 4 3 5 6 5 2 2 3 4 6 3 3 3 5 4 2 7 5 4
[176] 6 5 12 8 4 4 6 1 6 2 2 3 3 11 7 9 5 5 7 4 7 2 8 2 6

Example

which(x2%%2==1)

Output

 [1] 2 3 4 6 10 11 12 13 20 25 26 29 31 33 38 42 45 48 50
[20] 53 54 55 56 57 58 62 66 71 74 75 76 77 78 81 82 86 88 89
[39] 90 93 94 95 97 99 100 101 102 103 104 106 107 108 110 112 113 114 115
[58] 116 120 121 123 126 130 133 136 137 140 143 145 146 147 148 149 150 153 154
[77] 155 156 158 159 161 164 167 168 169 170 173 174 177 183 187 188 189 190 191
[96] 192 193 194 196

Example

 Live Demo

x3<-rpois(200,21)
x3

Output

[1] 16 23 22 31 19 28 20 24 18 12 21 18 16 25 22 18 29 19 17 21 19 29 14 20 23
[26] 12 17 22 17 18 16 16 21 27 22 24 29 24 16 27 26 17 26 21 21 22 21 16 21 23
[51] 19 23 16 27 15 11 18 14 26 22 18 22 33 17 18 17 21 17 20 30 18 21 18 29 21
[76] 19 20 30 23 20 12 20 21 18 19 21 24 24 22 27 24 18 20 22 23 17 22 21 18 19
[101] 18 14 27 24 20 16 28 19 23 21 22 21 22 17 19 17 26 24 30 23 21 14 18 20 21
[126] 24 18 17 19 22 20 29 18 25 18 25 26 19 24 14 25 24 23 33 17 20 16 36 22 12
[151] 29 28 11 16 23 19 15 27 23 17 22 17 29 20 21 29 19 23 26 28 19 15 15 23 26
[176] 18 27 24 30 28 23 22 23 23 21 19 18 27 19 28 28 24 25 21 21 26 21 18 26 19

Example

which(x3%%2==1)

Output

[1] 2 4 5 11 14 17 18 19 20 21 22 25 27 29 33 34 37 40 42
[20] 44 45 47 49 50 51 52 54 55 56 63 64 66 67 68 72 74 75 76
[39] 79 83 85 86 90 95 96 98 100 103 108 109 110 112 114 115 116 120 121
[58] 125 128 129 132 134 136 138 141 143 144 145 151 153 155 156 157 158 159 160
[77] 162 163 165 166 167 168 171 172 173 174 177 181 183 184 185 186 188 189 193
[96] 194 195 197 200

Example

 Live Demo

x4<-rpois(200,100)
x4

Output

[1] 100 103 88 98 86 108 91 84 80 100 109 87 101 88 104 93 95 98
[19] 97 96 104 101 105 99 105 88 93 107 93 102 97 93 128 108 106 91
[37] 90 114 93 110 105 87 94 70 99 112 91 96 107 88 115 104 81 91
[55] 99 108 93 98 122 112 103 92 81 100 95 105 99 115 100 102 107 105
[73] 105 111 94 89 87 112 104 88 92 99 105 105 95 110 82 125 101 86
[91] 94 99 104 110 83 120 92 101 100 94 108 99 103 72 108 112 73 84
[109] 97 102 114 105 101 101 105 107 89 88 97 84 86 106 111 96 102 108
[127] 102 111 90 79 102 109 91 111 93 106 96 103 92 116 97 109 106 107
[145] 108 105 103 106 90 79 109 91 110 111 95 110 118 109 101 107 106 110
[163] 116 97 103 113 95 123 101 110 106 94 92 90 92 95 101 106 94 92
[181] 103 106 93 116 89 84 87 90 94 102 92 107 86 107 97 98 89 86
[199] 93 104

Example

which(x4%%2==1)

Output

[1] 2 7 11 12 13 16 17 19 22 23 24 25 27 28 29 31 32 36 39
[20] 41 42 45 47 49 51 53 54 55 57 61 63 65 66 67 68 71 72 73
[39] 74 76 77 82 83 84 85 88 89 92 95 98 102 103 107 109 112 113 114
[58] 115 116 117 119 123 128 130 132 133 134 135 138 141 142 144 146 147 150 151
[77] 152 154 155 158 159 160 164 165 166 167 168 169 176 177 181 183 185 187 192
[96] 194 195 197 199

Example

 Live Demo

x5<-sample(0:9,200,replace=TRUE)
x5

Output

[1] 4 7 0 2 6 6 5 1 1 7 0 7 7 3 1 1 9 1 3 4 4 5 6 3 1 3 3 0 6 4 2 8 2 8 4 2 7
[38] 1 9 0 4 2 5 8 7 3 6 5 3 2 7 2 5 5 0 9 7 6 7 4 8 4 9 3 2 5 1 9 3 1 5 7 5 6
[75] 0 6 8 8 9 7 3 6 6 6 0 2 4 8 5 2 4 7 4 9 4 1 6 0 6 2 1 0 5 1 6 2 3 8 5 8 9
[112] 1 4 7 3 6 8 3 4 8 6 9 2 3 1 1 2 6 2 8 5 0 2 1 0 2 1 3 6 9 3 7 8 2 0 8 7 2
[149] 5 6 6 7 7 6 4 0 3 0 9 7 5 0 1 8 8 6 1 1 2 0 8 5 5 6 4 6 1 7 1 7 4 1 6 9 4
[186] 0 6 4 7 4 3 0 2 6 9 0 9 2 5 9

Example

which(x5%%2==1)

Output

[1] 2 7 8 9 10 12 13 14 15 16 17 18 19 22 24 25 26 27 37
[20] 38 39 43 45 46 48 49 51 53 54 56 57 59 63 64 66 67 68 69
[39] 70 71 72 73 79 80 81 89 92 94 96 101 103 104 107 109 111 112 114
[58] 115 118 122 124 125 126 131 134 137 138 140 141 142 147 149 152 153 157 159
[77] 160 161 163 167 168 172 173 177 178 179 180 182 184 189 191 195 197 199 200

Example

 Live Demo

x6<-sample(1:100,200,replace=TRUE)
x6

Output

  [1] 14 4 50 26 47 79 34 72 68 64 61 56 11 71 39 22 23 62
 [19] 97 51 26 84 18 79 7 47 63 73 65 2 80 5 5 84 74 43
 [37] 72 6 95 3 14 22 92 44 4 5 12 26 6 74 97 84 13 40
 [55] 15 92 56 79 73 39 37 93 70 62 68 70 8 73 59 13 56 22
 [73] 72 57 28 68 58 7 82 20 10 24 33 35 58 59 100 53 12 14
 [91] 9 9 40 17 71 61 81 77 47 15 52 18 21 47 56 74 52 9
[109] 94 50 40 25 26 3 67 94 53 17 90 28 85 61 18 28 60 61
[127] 9 15 99 53 31 69 92 45 82 80 44 4 68 45 55 22 51 79
[145] 91 100 65 100 37 82 26 7 59 77 87 19 63 83 77 76 44 17
[163] 4 56 64 45 45 11 22 20 91 74 17 72 32 22 74 96 37 52
[181] 86 73 76 91 15 97 82 30 11 81 20 38 38 73 56 28 33 62
[199] 66 70

Example

which(x6%%2==1)

Output

[1] 5 6 11 13 14 15 17 19 20 24 25 26 27 28 29 32 33 36 39
[20] 40 46 51 53 55 58 59 60 61 62 68 69 70 74 78 83 84 86 88
[39] 91 92 94 95 96 97 98 99 100 103 104 108 112 114 115 117 118 121 122
[58] 126 127 128 129 130 131 132 134 140 141 143 144 145 147 149 152 153 154 155
[77] 156 157 158 159 162 166 167 168 171 173 179 182 184 185 186 189 190 194 197
raja
Published on 17-Mar-2021 06:10:40
Advertisements