How to find the table of ordered frequencies of vector elements in R?


We can create table of frequencies of a vector element by using table function and the ordering can be done by using sort function. If we want to order the frequencies in decreasing order then decreasing argument can be used. For example, if we have a vector x then the table of ordered frequencies can be created as sort(table(x)).

Example

 Live Demo

x1<-sample(0:9,100,replace=TRUE)
x1

Output

[1] 1 4 3 9 8 7 9 5 8 0 6 7 9 0 0 1 5 9 3 0 0 3 7 4 6 8 4 9 3 0 0 2 9 0 2 3 5
[38] 7 8 4 0 7 6 6 0 5 2 2 6 6 0 8 5 2 2 8 3 1 9 4 8 8 4 1 6 4 9 2 4 2 5 0 5 2
[75] 3 2 1 1 8 2 3 6 3 5 3 9 5 9 7 8 2 5 0 6 1 3 4 2 4 5

Example

Table_Ordered_By_Frequency_x1<-sort(table(x1))
Table_Ordered_By_Frequency_x1

Output

x1
7 1 6 4 8 9 3 5 0 2
6 7 9 10 10 10 11 11 13 13

Example

 Live Demo

x2<-sample(0:9,200,replace=TRUE)
x2

Output

[1] 4 8 6 9 0 1 0 9 4 5 8 6 0 8 2 8 2 8 8 6 9 7 7 7 8 6 1 2 3 8 7 8 6 5 5 6 9
[38] 2 1 1 5 4 3 0 8 2 3 7 5 1 5 8 1 6 1 2 1 2 1 6 8 0 6 0 9 5 8 2 6 8 3 9 6 9
[75] 0 8 7 2 2 6 4 7 4 8 3 6 0 0 3 1 5 1 4 5 6 8 5 4 0 3 2 7 7 6 4 8 8 2 4 3 9
[112] 4 8 0 8 8 6 5 7 9 4 6 2 8 9 2 5 7 8 7 3 5 2 4 4 5 7 8 7 2 2 8 3 7 3 0 4 3
[149] 7 6 0 8 9 3 2 6 1 5 8 9 0 1 8 2 5 8 2 5 0 5 6 5 9 5 0 4 8 9 0 8 3 5 8 1 9
[186] 9 2 5 0 0 3 3 7 3 9 4 8 3 4 7

Example

Table_Ordered_By_Frequency_x2<-sort(table(x2),decreasing=T)
Table_Ordered_By_Frequency_x2

Output

x2
8 5 2 6 0 3 7 4 9 1
34 22 21 20 19 18 18 17 17 14

Example

 Live Demo

x3<-sample(1:10,200,replace=TRUE)
x3

Output

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

Example

Table_Ordered_By_Frequency_x3<-sort(table(x3),decreasing=T)
Table_Ordered_By_Frequency_x3

Output

x3
10 8 9 1 2 7 4 5 6 3
23 22 22 21 21 21 19 19 18 14

Example

 Live Demo

x4<-sample(11:99,200,replace=TRUE)
x4

Output

[1] 96 78 36 14 69 50 63 36 20 51 84 62 52 52 34 97 85 34 55 54 19 93 20 72 93
[26] 38 76 33 99 28 57 22 20 53 28 66 86 36 17 50 94 78 16 40 47 31 75 30 70 54
[51] 15 86 43 17 39 54 43 99 77 36 33 27 91 94 12 87 80 51 51 76 20 58 50 68 29
[76] 42 98 17 79 49 93 34 28 80 87 71 94 81 90 77 57 16 97 44 49 89 22 11 72 85
[101] 75 89 47 28 90 63 68 21 31 48 85 23 58 84 39 64 77 56 58 45 12 84 27 96 49
[126] 88 62 95 87 26 76 90 48 59 28 88 76 36 46 52 99 55 32 77 34 51 47 77 62 61
[151] 86 75 91 57 64 75 33 91 89 75 17 57 51 58 93 72 99 39 45 54 92 84 46 45 94
[176] 65 35 18 48 80 80 90 76 48 64 55 45 74 58 88 95 24 65 67 96 30 71 48 11 43

Example

Table_Ordered_By_Frequency_x4<-sort(table(x4))
Table_Ordered_By_Frequency_x4

Output

x4
14 15 18 19 21 23 24 26 29 32 35 38 40 42 44 53 56 59 61 66 67 69 70 74 79 81
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
92 98 11 12 16 22 27 30 31 46 63 65 68 71 78 95 97 33 39 43 47 49 50 52 55 62
1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3
64 72 85 86 87 88 89 91 96 17 20 34 45 54 57 80 84 90 93 94 99 28 36 48 51 58
3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5
75 76 77
5 5 5

Example

 Live Demo

x5<-rpois(200,10)
x5

Output

[1] 8 8 10 7 9 13 11 12 8 13 12 12 6 9 11 10 9 11 13 6 7 9 5 12 6
[26] 12 2 8 19 11 14 8 9 5 10 8 9 7 7 8 9 9 7 9 6 13 10 10 3 7
[51] 12 8 10 6 7 8 15 12 5 14 8 8 11 9 8 9 11 6 6 8 6 14 13 5 9
[76] 12 17 11 9 8 8 12 11 8 4 9 15 8 9 11 13 12 10 9 9 9 13 13 12 15
[101] 9 5 12 11 8 10 8 9 14 8 12 7 12 6 14 7 12 13 15 7 10 12 9 8 11
[126] 10 13 6 9 14 11 4 13 15 18 8 12 12 13 11 13 9 7 8 10 13 6 11 9 7
[151] 6 6 8 12 8 11 15 15 11 16 10 12 6 8 8 18 9 8 12 14 14 9 9 10 14
[176] 12 9 11 12 10 7 5 9 5 13 8 10 10 8 9 9 13 14 12 5 15 10 22 10 14

Example

Table_Ordered_By_Frequency_x5<-sort(table(x5),decreasing=T)
Table_Ordered_By_Frequency_x5

Output

x5
9 8 12 10 11 13 6 7 14 5 15 4 18 2 3 16 17 19 22
31 30 24 18 17 16 14 13 11 8 8 2 2 1 1 1 1 1 1

Example

 Live Demo

x6<-round(rnorm(200),0)
x6

Output

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

Example

 Live Demo

Table_Ordered_By_Frequency_x6<-sort(table(x6))
Table_Ordered_By_Frequency_x6

Output

x6
-3 2 -2 1 -1 0
1 9 16 50 54 70

Updated on: 07-Dec-2020

242 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements