How to find the number of unique values in a vector by excluding missing values in R?

R ProgrammingServer Side ProgrammingProgramming

If there exist missing values in an R vector then it is counted as a unique value in the vector, therefore the extraction of unique values cannot be done directly. For this purpose, we need to use unique with na.omit function. For example. If we have a vector called x with missing values then the extraction of unique values can be done as length(unique(na.omit(x))).

Example1

 Live Demo

x1<−sample(c(NA,rpois(5,1)),200,replace=TRUE)
x1

Output

[1] 0 0 1 1 1 NA NA 0 1 1 0 NA 1 0 0 NA 0 0 NA 1 0 0 0 1 1
[26] 0 0 0 0 0 NA 1 0 1 NA 1 0 1 1 1 1 1 1 0 1 1 0 0 0 0
[51] 1 NA 1 0 0 NA NA 0 0 0 1 NA 0 0 1 NA 1 1 0 1 NA 0 0 1 NA
[76] 1 NA 1 0 NA 1 NA 0 0 1 0 0 NA 1 0 1 0 1 0 1 1 0 0 0 0
[101] 0 0 1 1 0 1 0 NA 0 NA 0 0 0 1 0 0 0 0 NA NA NA 0 1 0 0
[126] 1 0 1 0 0 0 0 1 0 1 0 0 NA 1 1 1 0 0 0 0 1 1 0 NA 0
[151] 0 NA 1 0 0 0 1 1 0 NA 1 1 0 0 1 0 0 NA 0 0 1 0 0 0 1
[176] 1 1 0 NA NA 0 1 1 NA 0 NA 0 1 1 0 0 1 0 1 0 NA 1 1 1 0

Example

length(unique(na.omit(x1)))

Output

[1] 2

Example2

 Live Demo

x2<−sample(c(NA,0,1,2),200,replace=TRUE)
x2

Output

[1] NA 0 0 NA 0 0 0 NA 2 1 2 0 0 NA 2 1 1 0 NA 2 2 NA 2 NA 1
[26] 1 2 1 1 1 2 0 2 0 0 0 1 NA NA 1 1 2 1 0 1 0 NA 0 0 1
[51] NA NA 0 0 0 0 NA 2 0 NA 0 2 0 NA 0 NA NA 2 0 0 0 NA 2 NA 1
[76] NA 2 0 1 2 0 2 NA 2 NA NA 1 1 0 NA 2 NA 0 0 NA NA 0 NA 1 NA
[101] 2 2 2 1 NA 1 1 2 0 1 2 2 2 2 1 0 2 NA 1 NA 1 1 NA 0 NA
[126] 0 NA 1 NA 2 NA 1 NA NA 2 NA 2 0 1 2 1 0 1 1 0 NA NA 1 0 1
[151] NA 0 0 2 NA NA 2 NA NA 1 NA 2 0 NA 1 NA 0 NA 0 0 2 1 0 NA 2
[176] 1 0 2 1 1 1 0 0 0 NA NA NA 0 0 NA 1 0 NA NA NA NA 1 NA 0 2

Example

length(unique(na.omit(x2)))

Output

[1] 3

Example3

 Live Demo

x3<−sample(c(NA,0:9),200,replace=TRUE)
x3

Output

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

Example

length(unique(na.omit(x3)))

Output

[1] 10

Example4

 Live Demo

x4<−sample(c(NA,1:10),200,replace=TRUE)
x4

Output

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

Example

length(unique(na.omit(x4)))

Output

[1] 10

Example5

 Live Demo

x5<−sample(c(NA,5,10,15,20,25),200,replace=TRUE)
x5

Output

[1] 5 5 15 5 15 10 15 15 20 15 5 10 25 5 NA NA 20 25 10 NA NA 20 10 10 15
[26] 10 NA 25 10 25 5 15 NA 15 NA 15 15 5 10 5 5 NA 15 NA 10 5 10 NA NA NA
[51] 25 5 10 NA 10 10 15 20 20 5 25 20 20 20 25 20 15 NA 10 5 15 15 10 5 5
[76] 5 NA 25 5 10 20 NA 25 10 20 20 NA NA 10 5 25 25 20 25 15 10 NA 25 5 15
[101] 25 NA 25 NA 5 NA 10 5 25 25 NA 15 20 5 20 5 15 15 NA 25 25 5 20 25 NA
[126] 25 20 5 25 5 15 25 10 15 NA 10 NA 15 25 15 25 NA NA 15 25 NA 25 25 NA 20
[151] NA NA 20 5 25 20 15 25 10 20 5 10 NA 25 25 NA 5 15 25 20 10 15 NA 5 5
[176] 10 NA 5 10 10 5 25 NA 5 5 10 10 5 20 10 20 NA 25 5 5 10 25 20 25 20

Output

length(unique(na.omit(x5)))

Output

[1] 5

Example6

 Live Demo

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

Output

[1] −2.4891929 −2.4891929 −0.1627979 −1.8763364 −0.6250307 NA
[7] −0.1627979 NA −0.1627979 NA −0.6250307 −0.6250307
[13] −2.4891929 −0.1627979 −2.4891929 NA NA −2.4891929
[19] −2.4891929 −0.1627979 −2.4891929 −0.6250307 −1.8763364 −1.8763364
[25] −1.8763364 −2.4891929 −2.4891929 −2.4891929 −2.4891929 −1.8763364
[31] −2.4891929 NA −0.6250307 NA −1.8763364 −0.1627979
[37] −2.4891929 −0.6250307 −2.4891929 −0.6250307 −1.8763364 −1.8763364
[43] NA −2.4891929 −0.6250307 −0.6250307 −0.6250307 −0.6250307
[49] −0.1627979 −0.6250307 −0.6250307 −2.4891929 −1.8763364 NA
[55] −2.4891929 −0.1627979 −0.2147164 −1.8763364 −2.4891929 −1.8763364
[61] −0.2147164 −1.8763364 −2.4891929 −0.6250307 −0.6250307 −0.6250307
[67] −0.1627979 −0.6250307 −2.4891929 −2.4891929 −0.6250307 −0.2147164
[73] −2.4891929 −0.1627979 −0.1627979 −0.2147164 −0.2147164 −0.6250307
[79] −2.4891929 −0.6250307

Example

length(unique(na.omit(x6)))

Output

[1] 5
raja
Published on 09-Feb-2021 12:12:07
Advertisements