How to find the position of NA in an R vector?

R ProgrammingServer Side ProgrammingProgramming

When we have NA’s/missing values in an R vector then we want to replace those NA’s and for this purpose we might be needing the position of those values. These positions will be helpful especially in situations when we want to manually replace the missing values. The replacement can be done by using which function with is.na.

Example1

 Live Demo

x1<−sample(c(NA,0,1),100,replace=TRUE)
x1

Output

[1] NA NA 1 0 1 0 1 0 0 1 NA 1 NA NA 0 NA 1 0 NA 1 1 1 0 NA 0
[26] 1 NA NA NA 0 1 NA 1 0 1 NA 0 0 1 0 1 NA 1 0 1 0 0 NA 1 1
[51] 0 0 NA 0 0 1 1 NA 1 1 1 0 0 1 1 1 0 0 0 0 NA 1 NA NA NA
[76] NA 1 0 NA NA 1 1 NA 0 NA 1 1 0 0 NA 1 NA 0 NA NA 0 1 NA 0 NA

Example

which(is.na(x1))

Output

[1] 1 2 11 13 14 16 19 24 27 28 29 32 36 42 48 53 58 71 73
[20] 74 75 76 79 80 83 85 90 92 94 95 98 100

Example2

 Live Demo

x2<−sample(c(NA,0:9),150,replace=TRUE)
x2

Output

[1] 6 4 4 1 NA 5 1 NA 9 7 5 5 7 5 2 1 2 4 3 9 5 NA 0 1 9
[26] 1 6 7 1 8 8 1 4 7 1 3 2 6 4 9 NA 4 3 8 7 1 0 6 7 8
[51] 6 4 6 6 6 8 3 6 3 9 4 4 NA 9 0 6 8 7 1 0 9 0 3 3 8
[76] 5 1 3 NA 4 5 6 7 9 NA 0 6 3 9 9 5 NA 1 7 3 4 5 NA 2 8
[101] 4 9 5 2 NA 6 9 0 4 5 3 6 5 0 3 8 8 4 5 9 5 1 4 4 2
[126] 2 5 6 2 8 9 2 NA 1 2 1 9 1 7 4 0 8 8 0 8 1 7 3 6 9

Example

which(is.na(x2))

Output

[1] 5 8 22 41 63 79 85 92 98 105 133

Example3

 Live Demo

x3<−sample(c(NA,10,99),150,replace=TRUE)
x3

Output

[1] 99 99 10 10 NA 99 10 99 NA NA 99 NA NA 99 NA 99 99 10 10 99 NA 10 NA 99 10
[26] NA NA NA NA NA 10 10 99 10 10 10 99 99 10 10 99 99 NA 10 NA NA 10 10 99 99
[51] 99 NA 99 99 99 99 NA 99 NA NA NA 99 NA 99 99 99 99 99 10 10 99 10 NA NA 10
[76] 10 99 10 99 NA NA 10 99 10 99 NA 10 99 NA 99 10 99 99 NA NA 10 10 99 NA 10
[101] 99 NA NA 99 NA 10 99 99 10 NA 99 10 NA NA 10 NA 10 NA 10 99 NA 99 NA NA 10
[126] 10 NA 10 NA 99 NA 99 NA NA 10 10 10 99 NA 99 10 10 NA NA 10 99 NA 10 10 99

Example

which(is.na(x3))

Output

which(is.na(x3))

Example4

 Live Demo

x4<−sample(c(NA,rnorm(5)),80,replace=TRUE)
x4

Output

[1]  NA 0.4902959 NA -0.1427441 -0.0157058 0.3530351
[7]  NA    NA −0.0157058 −0.1427441 −0.0157058  NA
[13] −0.1427441 −0.1427441 −0.0157058 0.4902959 0.3530351 NA
[19] 0.4902959 0.3530351 0.3530351 0.4902959 NA 0.2005032
[25] NA −0.0157058 0.3530351 0.2005032 −0.1427441 −0.1427441
[31] 0.2005032 −0.0157058 0.4902959 0.2005032 −0.0157058 −0.1427441
[37] 0.4902959 NA NA 0.4902959 −0.0157058 −0.1427441
[43] 0.3530351 −0.1427441 −0.1427441 NA 0.4902959 NA
[49] 0.3530351 0.3530351 0.4902959 0.2005032 0.3530351 −0.0157058
[55] 0.2005032 NA 0.4902959 −0.0157058 0.3530351 0.3530351
[61] 0.2005032 0.3530351 0.4902959 0.4902959 −0.0157058 0.4902959
[67] −0.0157058 0.4902959 −0.1427441 −0.1427441 −0.1427441 −0.1427441
[73] 0.4902959 0.4902959 0.2005032 0.3530351 0.4902959 −0.1427441
[79] 0.4902959 0.4902959

Example

which(is.na(x4))

Output

[1] 1 3 7 8 12 18 23 25 38 39 46 48 56

Example5

 Live Demo

x5<−sample(c(NA,rnorm(5,25,3.1)),80,replace=TRUE)
x5

Output

[1] NA 20.85061 23.28236 27.07661 23.92421 26.08445 26.08445 27.07661
[9] NA 26.08445 26.08445 26.08445 23.92421 23.28236 27.07661 27.07661
[17] NA NA 26.08445 23.28236 26.08445 23.28236 27.07661 23.92421
[25] NA NA 20.85061 NA 26.08445 23.28236 NA NA
[33] 23.92421 27.07661 23.28236 23.28236 20.85061 23.28236 23.92421 27.07661
[41] 20.85061 27.07661 23.28236 23.92421 NA NA 23.28236 23.28236
[49] 23.92421 26.08445 NA NA NA 23.92421 20.85061 27.07661
[57] 23.28236 23.28236 20.85061 23.28236 27.07661 NA 20.85061 27.07661
[65] 20.85061 NA 20.85061 23.28236 20.85061 23.92421 27.07661 26.08445
[73] 27.07661 23.92421 27.07661 20.85061 26.08445 26.08445 23.28236 20.85061

Example

which(is.na(x5))

Example6

 Live Demo

x6<−sample(c(NA,rpois(10,5)),150,replace=TRUE)
x6

Output

[1] 6 5 4 6 NA 4 3 4 NA 6 6 3 3 5 9 3 6 3 3 4 4 6 6 4 6
[26] 5 6 6 6 5 4 9 6 4 4 3 6 3 9 3 6 4 9 6 4 6 4 NA 6 6
[51] 6 6 3 6 6 3 4 NA 6 4 4 4 6 6 4 6 6 4 6 5 6 6 4 6 3
[76] 5 4 NA 6 3 6 5 NA 5 4 5 4 9 6 5 NA 4 6 NA 5 NA 3 6 NA 3
[101] 3 6 6 NA 6 6 4 4 4 3 9 NA 9 3 4 4 5 NA 6 6 3 4 6 NA 6
[126] 3 4 6 3 3 6 3 4 5 4 4 6 4 3 3 4 5 5 4 3 4 5 6 9 3

Example

which(is.na(x6))

Output

[1] 5 9 48 58 78 83 91 94 96 99 104 112 118 124

Example7

 Live Demo

x7<−sample(c(NA,rexp(10,2.01)),80,replace=TRUE)
x7

Output

[1] 0.07553376 0.42805544 0.07934303 2.82645498 0.07553376 0.07934303
[7] NA NA 0.42805544 0.39840736 0.07553376 0.13041258
[13] 0.42805544 0.13041258 0.68973943 0.13041258 0.89757427 NA
[19] 0.07934303 0.68973943 0.89757427 0.13041258 0.07934303 0.06858492
[25] 0.13041258 0.89757427 0.13041258 0.68973943 0.39840736 2.82645498
[31] 0.13041258 0.06858492 0.72781631 0.89757427 0.13041258 0.39840736
[37] 0.42805544 0.42805544 0.07553376 0.72781631 0.13041258 0.39840736
[43] 0.68973943 0.72781631 0.07553376 0.13041258 0.42805544 0.13041258
[49] 2.82645498 0.72781631 0.42805544 0.13041258 0.06858492 0.39840736
[55] 0.39840736 0.07934303 0.06858492 2.82645498 NA 0.07934303
[61] NA NA 0.07934303 0.42805544 0.72781631 NA
[67] 0.39840736 0.06858492 0.89757427 0.42805544 0.72781631 0.68973943
[73] 0.07553376 0.06858492 0.13041258 0.42805544 0.13041258 2.82645498
[79] 0.13041258 0.07934303

Example

which(is.na(x7))

Output

[1] 7 8 18 59 61 62 66

Example8

 Live Demo

x8<−sample(c(NA,rnorm(10,1,0.25)),80,replace=TRUE)
x8

Output

[1] 0.9030808 0.8823065 0.6860899 0.5996056 0.7197702 0.6860899 0.6860899
[8] 0.6860899 0.9454482 0.5708919 0.5708919 0.8432231 0.6860899 0.9030808
[15] 0.9030808 0.9030808 0.4306264 0.8823065 0.5996056 0.7197702 0.9454482
[22] 0.6860899 0.8432231 0.4306264 0.9810333 0.7197702 0.5708919 NA
[29] 0.9810333 0.9454482 0.7197702 0.9454482 0.9454482 0.7197702 0.4306264
[36] 0.8823065 0.6860899 0.7197702 0.9454482 0.6860899 0.5708919 0.9454482
[43] 0.6860899 0.9030808 NA 0.9030808 0.4306264 0.8432231 0.6860899
[50] NA 0.9030808 NA 0.9810333 0.9454482 0.7197702 0.8432231
[57] NA 0.5708919 NA 0.8823065 0.4306264 0.9810333 0.8432231
[64] 0.8823065 0.5996056 0.8432231 0.5996056 0.8823065 0.7197702 0.5996056
[71] 0.5996056 0.9454482 0.4306264 0.4306264 0.4306264 NA 0.9810333
[78] 0.8823065 0.9454482 0.9454482

Example

which(is.na(x8))

Output

[1] 28 45 50 52 57 59 76
raja
Published on 05-Feb-2021 08:16:28
Advertisements