How to find the frequency of repeated and unique values in a vector in R?


If we unique values in a vector in R and they are repeated then we can find the frequency of those unique values, this will help us to understand the distribution of the values in the vector. On the basis of that distribution analysis, we can proceed with the further analysis that could be used. This can be done with the help of rle function.

Example

 Live Demo

x1<-sample(0:2,20,replace=TRUE)
x1

Output

[1] 1 0 1 2 0 2 1 0 1 1 0 2 0 0 1 0 2 0 2 2

Example

with(rle(x1),table(lengths[values==1]))

Output

1 2
4 1

Example

 Live Demo

x2<-sample(0:2,140,replace=TRUE)
x2

Output

[1] 0 2 2 2 2 0 2 1 1 2 1 1 0 1 2 2 1 0 1 1 0 2 0 2 2 0 2 1 1 1 0 0 0 1 1 0 1
[38] 0 0 1 1 0 2 2 2 0 1 1 0 1 0 1 0 1 2 2 0 1 2 0 2 2 1 1 0 2 1 2 1 2 2 1 2 1
[75] 0 0 1 1 1 2 2 1 1 2 1 2 1 0 2 0 0 0 1 2 2 2 1 0 2 2 1 1 0 2 1 2 2 1 0 0 1
[112] 0 2 1 0 2 0 2 1 2 0 1 1 2 1 1 1 0 2 1 2 0 2 2 2 0 1 1 2 0

Example

with(rle(x2),table(lengths[values==1]))

Output

 1  2 3
21 11 3

Example

 Live Demo

x3<-sample(0:5,140,replace=TRUE)
x3

Output

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

Example

with(rle(x3),table(lengths[values==1]))

Output

 1 2 3
16 3 1

Example

with(rle(x3),table(lengths[values==2]))

Output

 1 2
17 1

Example

with(rle(x3),table(lengths[values==3]))

Output

 1 2 3
16 1 2

Example

with(rle(x3),table(lengths[values==4]))

Output

1 2
9 6

Example

with(rle(x3),table(lengths[values==5]))

Output

 1 2 3
14 2 2

Example

with(rle(x3),table(lengths[values==0]))

Output

 1 2
15 6

Example

 Live Demo

x4<-sample(1:10,150,replace=TRUE)
x4

Output

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

Example

with(rle(x4),table(lengths[values==1]))

Output

 1 2
19 1

Example

with(rle(x4),table(lengths[values==2]))

Output

 1 2
11 1

Example

with(rle(x4),table(lengths[values==3]))

Output

1 2
7 1

Example

with(rle(x4),table(lengths[values==4]))

Output

 1 2
11 1

Example

with(rle(x4),table(lengths[values==5]))

Output

1
15

Example

with(rle(x4),table(lengths[values==6]))

Output

 1 2
18 1

Example

with(rle(x4),table(lengths[values==7]))

Output

 1 2
13 1

Example

with(rle(x4),table(lengths[values==8]))

Output

 1 2
17 3

Example

with(rle(x4),table(lengths[values==9]))

Output

1 2
7 1

Example

with(rle(x4),table(lengths[values==10]))

Output

1
12

Example

 Live Demo

x5<-sample(1001:1005,120,replace=TRUE)
x5

Output

[1] 1004 1004 1005 1001 1001 1002 1004 1001 1001 1004 1004 1002 1005 1003 1002
[16] 1004 1004 1004 1005 1005 1004 1003 1003 1005 1001 1005 1003 1004 1003 1003
[31] 1005 1004 1001 1002 1002 1004 1003 1002 1003 1004 1001 1001 1005 1001 1001
[46] 1001 1003 1002 1004 1004 1002 1002 1002 1002 1002 1005 1005 1001 1004 1004
[61] 1004 1001 1003 1004 1001 1002 1004 1005 1004 1002 1004 1003 1003 1001 1002
[76] 1002 1003 1001 1002 1001 1002 1001 1005 1005 1003 1003 1002 1004 1002 1004
[91] 1001 1005 1005 1005 1002 1004 1005 1001 1002 1003 1002 1004 1002 1002 1005
[106] 1005 1005 1005 1003 1004 1004 1005 1001 1002 1003 1004 1005 1003 1002 1004

Example

with(rle(x5),table(lengths[values==1001]))

Output

 1 2 3
12 3 1

Example

with(rle(x5),table(lengths[values==1002]))

Output

 1 2 5
16 3 1

Example

with(rle(x5),table(lengths[values==1003]))

Output

 1 2
11 4

Example

with(rle(x5),table(lengths[values==1004]))

Output

 1 2 3
16 4 2

Example

with(rle(x5),table(lengths[values==1005]))

Output

 1 2 3 4
10 3 1 1

Updated on: 17-Oct-2020

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements