如何在R向量中查找NA的位置?


当R向量中存在NA/缺失值时,我们需要替换这些NA,为此,我们可能需要这些值的位置。这些位置在我们需要手动替换缺失值的情况下特别有用。可以使用`which`函数结合`is.na`来进行替换。

示例1

 在线演示

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

输出

[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

示例

which(is.na(x1))

输出

[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

示例2

 在线演示

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

输出

[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

示例

which(is.na(x2))

输出

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

示例3

 在线演示

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

输出

[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

示例

which(is.na(x3))

输出

which(is.na(x3))

示例4

 在线演示

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

输出

[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

示例

which(is.na(x4))

输出

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

示例5

 在线演示

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

输出

[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

示例

which(is.na(x5))

示例6

 在线演示

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

输出

[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

示例

which(is.na(x6))

输出

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

示例7

 在线演示

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

输出

[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

示例

which(is.na(x7))

输出

[1] 7 8 18 59 61 62 66

示例8

 在线演示

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

输出

[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

示例

which(is.na(x8))

输出

[1] 28 45 50 52 57 59 76

更新于:2021年2月5日

1K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告