How to find the position of a non-NA value in an R vector?

R ProgrammingServer Side ProgrammingProgramming

An NA value in R represents not available or missing value, therefore, it is not useful for any type of mathematical operations. Hence, non-NA values are the values that matters and we might want to find the position of these values. We can find the position of non-NA values in R using !is.na which means values that are not NA.

Examples

set.seed(1)
x1<-sample(c(1,2,3,4,NA),50,replace=TRUE)
x1
[1] 1 4 1 2 NA 3 2 3 3 1 NA NA 2 2 1 NA NA 1 1 NA NA 2 2 1 4
[26] 1 4 3 2 2 4 4 4 2 4 1 1 4 1 2 3 2 2 NA 2 1 3 3 4 3
Non_NA_Value_position_x1<-which(!is.na(x1))
Non_NA_Value_position_x1
[1] 1 2 3 4 6 7 8 9 10 13 14 15 18 19 22 23 24 25 26 27 28 29 30 31 32
[26] 33 34 35 36 37 38 39 40 41 42 43 45 46 47 48 49 50
x2<-sample(c(5,15,28,NA,27,35),100,replace=TRUE)
x2
[1] 5 NA 27 5 5 35 NA 27 27 NA 35 27 NA NA 5 27 27 35 5 5 28 35 15 15 28
[26] 35 15 NA 28 27 15 15 5 28 28 15 15 27 15 27 NA 27 NA 35 5 28 15 28 28 5
[51] 27 35 35 35 NA NA 5 27 27 35 5 28 35 28 35 28 28 NA 5 5 NA 15 35 5 15
[76] 28 NA 5 28 27 28 NA 15 5 NA 5 NA 15 27 15 15 15 28 5 15 28 28 28 35 28
Non_NA_Value_position_x2<-which(!is.na(x2))
Non_NA_Value_position_x2
[1] 1 3 4 5 6 8 9 11 12 15 16 17 18 19 20 21 22 23 24
[20] 25 26 27 29 30 31 32 33 34 35 36 37 38 39 40 42 44 45 46
[39] 47 48 49 50 51 52 53 54 57 58 59 60 61 62 63 64 65 66 67
[58] 69 70 72 73 74 75 76 78 79 80 81 83 84 86 88 89 90 91 92
[77] 93 94 95 96 97 98 99 100
x3<-sample(c(NA,1:5),100,replace=TRUE)
x3
[1] 4 4 3 2 1 1 5 4 1 5 1 4 2 NA 2 NA 5 4 5 2 2 3 5 1 5
[26] NA 5 NA 3 4 5 1 1 3 3 4 5 2 3 3 3 3 2 3 5 1 4 2 5 4
[51] 1 5 2 4 3 3 1 4 4 5 1 4 4 2 1 3 NA 5 2 3 3 1 3 4 NA
[76] 4 5 4 NA 2 2 1 3 3 2 5 3 NA 5 3 5 NA 2 2 5 4 4 NA NA 5
Non_NA_Value_position_x3<-which(!is.na(x3))
Non_NA_Value_position_x3
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 15 17 18 19 20 21
[20] 22 23 24 25 27 29 30 31 32 33 34 35 36 37 38 39 40 41 42
[39] 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
[58] 62 63 64 65 66 68 69 70 71 72 73 74 76 77 78 80 81 82 83
[77] 84 85 86 87 89 90 91 93 94 95 96 97 100
x4<-sample(c(NA,1,3),100,replace=TRUE)
x4
[1] 1 3 3 3 1 NA 3 3 1 NA NA 1 3 1 1 3 1 3 3 NA 3 1 1 3 3
[26] 1 NA NA 1 NA 1 NA NA NA 3 1 NA 1 NA 3 NA 1 NA NA 3 1 NA NA 3 NA
[51] NA 1 NA 1 3 NA NA 3 NA 3 NA 3 NA NA NA NA 1 NA 1 1 3 1 3 1 1
[76] 1 3 1 1 NA 3 1 3 NA NA 3 3 1 NA NA 1 NA 1 1 1 1 3 1 NA 3
Non_NA_Value_position_x4<-which(!is.na(x4))
Non_NA_Value_position_x4
[1] 1 2 3 4 5 7 8 9 12 13 14 15 16 17 18 19 21 22 23
[20] 24 25 26 29 31 35 36 38 40 42 45 46 49 52 54 55 58 60 62
[39] 67 69 70 71 72 73 74 75 76 77 78 79 81 82 83 86 87 88 91
[58] 93 94 95 96 97 98 100
x5<-sample(c(NA,5),100,replace=TRUE)
x5
[1] NA NA NA 5 NA 5 NA 5 NA 5 5 5 NA NA 5 5 5 NA 5 NA 5 5 NA NA NA
[26] NA NA NA 5 5 NA 5 5 5 NA 5 5 NA 5 5 5 5 5 5 NA NA 5 5 5 NA
[51] 5 NA 5 5 NA NA 5 5 NA 5 NA NA NA 5 NA NA 5 NA NA 5 5 5 5 NA 5
[76] NA 5 NA 5 NA 5 NA NA 5 NA 5 5 NA 5 5 NA 5 NA NA 5 5 5 5 NA NA
Non_NA_Value_position_x5<-which(!is.na(x5))
Non_NA_Value_position_x5
[1] 4 6 8 10 11 12 15 16 17 19 21 22 29 30 32 33 34 36 37 39 40 41 42 43 44
[26] 47 48 49 51 53 54 57 58 60 64 67 70 71 72 73 75 77 79 81 84 86 87 89 90 92
[51] 95 96 97 98
Non_NA_Value_position_x6<-which(!is.na(x6))
Non_NA_Value_position_x6
[1] 1 2 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 22 23 25 27 28 29 30
[26] 31 32 34 35 36 37 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
[51] 58 59 61 62 63 64 65 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
[76] 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
raja
Published on 24-Aug-2020 11:53:26
Advertisements