How to find the number of distinct values in an R vector?


When we have repeated elements in an R vector and the vector size is large then we might want to know the distinct values in that vector. This will help us to understand the unique values we have in our vector, so that we can create the appropriate chart and perform the appropriate analysis using that vector. This can be done by using length function with unique.

Examples

 Live Demo

> x1<-sample(1:5,50,replace=TRUE)
> x1

Output

[1] 2 5 5 3 2 4 3 3 1 4 5 4 5 3 3 1 1 2 5 1 3 2 4 1 3 1 5 4 2 5 5 3 2 4 1 1 1 3
[39] 3 5 2 5 4 2 2 2 4 1 1 1


> length(unique(x1))

Output

[1] 5

Example

 Live Demo

> x2<-rpois(100,5)
> x2

Output

[1] 2 2 9 5 4 3 2 6 5 11 5 2 3 5 0 5 3 9 6 3 5 6 5 9 6
[26] 4 2 3 7 4 5 6 6 3 7 9 5 5 3 0 8 3 1 9 4 3 4 5 4 5
[51] 7 2 5 6 6 6 5 10 4 7 6 5 8 5 1 6 6 3 6 5 4 6 7 6 4
[76] 3 4 8 7 6 9 11 5 1 4 4 2 2 4 4 3 6 4 9 6 4 3 2 12 4
> length(unique(x2))

Output

[1] 13

Example

> x3<-rpois(100,2)
> length(unique(x3))
[1] 6
> x3

Output

[1] 3 4 2 3 1 1 2 0 1 2 3 4 1 3 0 2 1 2 4 1 1 2 3 2 3 2 2 2 1 0 1 1 3 3 1 2 2
[38] 2 1 2 0 4 0 2 3 2 2 2 3 1 5 2 4 4 3 2 2 0 2 2 4 3 3 2 0 3 2 2 0 1 2 3 0 2
[75] 4 3 1 3 1 2 2 0 3 2 3 0 3 1 1 3 0 0 1 2 2 1 1 1 2 3


> length(unique(x3))

Output

[1] 6

Example

 Live Demo

> x4<-rnorm(50,mean=2,sd=10)
> x4

Output

[1] -9.6766233 1.9169099 3.2885540 0.5412437 0.3608904 19.6355200
[7] 9.6258651 13.1143108 -7.2320695 3.6434184 13.5482519 1.4347858
[13] -19.2936065 5.4484576 -17.0495545 -6.1117015 15.2400432 8.1563685
[19] 12.9166896 5.0660486 0.8984124 -7.2431277 17.9291375 2.4501060
[25] -5.1512840 10.6522310 12.7444096 20.9565477 -4.0299730 -1.9086782
[31] -2.1622203 -1.7565742 -1.6663095 -0.9567745 16.4182041 -4.9753829
[37] -1.8816751 8.5253645 13.2477245 -5.7211080 -3.0808622 7.2362059
[43] 12.1775423 -0.5116459 -12.2999345 19.0912103 16.3506957 -5.1037115
[49] 1.3493243 -15.5946874
> length(unique(x4))

Output

[1] 50

Example

 Live Demo

> x5<-runif(50,2,5)
> x5

Output

[1] 4.146702 3.055000 4.839670 4.229320 2.152358 4.941094 2.653467 2.108588
[9] 2.782112 4.161247 2.743683 2.704747 2.063036 2.793862 4.075405 2.104008
[17] 2.188358 3.193118 3.470085 3.893146 4.170213 2.230297 3.264338 4.921180
[25] 4.441417 2.671303 3.469227 2.034021 2.775807 3.363796 4.222930 4.971980
[33] 2.996615 4.834374 4.885560 4.697832 3.478148 4.354806 4.409357 4.033303
[41] 3.743833 2.992004 2.003944 2.197406 2.257856 2.039329 3.007237 2.357793
[49] 3.780786 2.111938
> length(unique(x5))

Output

[1] 50

Example

 Live Demo

> x6<-rpois(100,10)
> x6

Output

[1] 2 12 7 25 12 5 6 13 15 4 11 10 10 7 12 9 8 14 9 9 12 12 7 10 13
[26] 9 8 6 7 17 17 9 16 12 6 7 8 10 9 7 11 7 11 15 4 13 15 5 13 8
[51] 8 12 13 12 8 11 9 15 7 13 7 10 9 8 14 10 14 6 10 6 6 8 15 7 9
[76] 10 12 11 11 10 14 9 7 12 10 14 7 6 9 9 11 11 10 12 12 12 13 14 9 8
> length(unique(x6))

Output

[1] 16

Example

 Live Demo

> x7<-rep(LETTERS[1:10],5)
> x7

Output

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "A" "B" "C" "D" "E" "F" "G" "H" "I"
[20] "J" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "A" "B" "C" "D" "E" "F" "G" "H"
[39] "I" "J" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
> length(unique(x7))

Output

[1] 10

Example

 Live Demo

> x8<-sample(LETTERS[1:26],50,replace=TRUE)
> x8

Output

[1] "U" "P" "G" "Y" "J" "S" "Z" "C" "Y" "M" "P" "S" "N" "S" "J" "B" "P" "K" "S"
[20] "X" "C" "G" "V" "W" "X" "L" "H" "W" "M" "T" "U" "R" "J" "B" "I" "S" "L" "J"
[39] "X" "L" "Y" "W" "F" "H" "W" "M" "K" "M" "B" "H"
> length(unique(x8))

Output

[1] 21

Example

 Live Demo

> x9<-sample(letters[1:26],50,replace=TRUE)
> x9

Output

[1] "l" "s" "v" "w" "t" "c" "d" "u" "u" "l" "x" "m" "g" "v" "x" "z" "v" "w" "c"
[20] "e" "t" "t" "v" "o" "w" "f" "j" "m" "y" "w" "l" "q" "r" "t" "g" "n" "j" "p"
[39] "a" "x" "i" "c" "k" "h" "z" "d" "q" "e" "w" "j"
> length(unique(x9))

Output

[1] 25

Example

 Live Demo

> x10<-rbinom(50,10,0.5)
> x10

Output

[1] 2 4 4 7 6 5 5 4 1 8 6 4 6 7 5 6 4 6 8 5 5 6 5 2 3 3 3 5 3 5 4 5 7 5 8 5 7 6
[39] 7 4 5 2 3 5 4 6 1 7 4 5
> length(unique(x10))

Output

[1] 8

Updated on: 07-Sep-2020

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements