How to create a frequency table in R that includes zero frequency for value that are not available?

R ProgrammingServer Side ProgrammingProgramming

Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

When we use table function in R, the output shows the frequency of values that are available in the vector or in column of the data frame. If we want to create the table with the frequency zero for values that are not part of the vector or the column then first we need to convert them to factor first and then use the table function.

Example1

Live Demo

x1<−c(1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,4,4,4,6,6)
x1<−factor(x1,levels=c(1:6))
table(x1)

Output

x1
1 2 3 4 5 6
7 5 4 4 0 2

Example2

Live Demo

x2<−sample(c(1,2,3,5,6,7),200,replace=TRUE)
x2

Output

[1] 2 3 1 7 7 2 3 5 5 3 1 3 7 6 7 2 6 3 2 1 1 1 5 7 3 6 6 5 5 5 5 2 2 7 7 6 3
[38] 7 1 6 2 7 3 6 5 2 6 7 7 2 3 1 3 3 7 1 1 5 7 5 3 1 6 2 3 1 2 5 3 3 1 3 7 2
[75] 1 6 2 1 6 7 7 2 1 3 7 6 7 6 3 5 6 7 7 7 3 3 2 6 2 1 3 7 6 1 3 5 5 3 7 1 3
[112] 2 1 2 3 1 3 6 3 3 5 3 2 2 7 7 6 3 2 2 3 5 3 7 6 6 6 6 2 7 1 7 7 5 3 6 5 3
[149] 5 1 6 2 5 5 1 6 3 6 3 6 5 5 1 1 6 3 3 7 2 3 5 3 1 1 7 1 3 2 3 3 7 7 7 3 5
[186] 1 3 7 6 7 5 3 6 3 6 1 1 1 3 7

Example

x2<−factor(x2,levels=c(1:7))
table(x2)

Output

x2
1 2 3 4 5 6 7
32 26 48 0 26 31 37

Example3

Live Demo

x3<−sample(c(0,1,2,4,5,6),500,replace=TRUE)
x3

Output

[1] 5 4 4 6 6 4 2 2 5 0 1 1 1 1 0 2 2 1 0 1 5 2 4 5 0 5 4 2 5 2 6 0 1 6 4 2 4
[38] 6 0 6 2 6 5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2 0 2 1 5 0 1 1 2 1 0 0 0 1 5 1 5 1
[482] 5 2 0 2 5 5 1 1 0 5 1 2 1 5 2 2 5 1 0

Example

x4<−factor(x4,levels=c(0:5))
table(x4)

Output

x4
0 1 2 3 4 5
128 135 120 0 0 117

Example5

Live Demo

x5<−sample(c(0,1,5,10),500,replace=TRUE)
x5

Output

[1] 1 0 1 0 5 5 10 0 0 1 10 0 0 5 10 1 5 1 10 0 1 0 1 5 1
[26] 5 10 5 10 1 10 1 0 1 1 0 5 5 5 0 10 5 10 5 5 0 5 0 0 1
[51] 0 5 5 0 1 10 10 0 0 1 0 0 0 10 5 0 1 1 0 0 5 10 0 5 1
[76] 10 10 5 1 1 0 0 0 0 0 5 5 0 10 0 10 0 5 0 10 5 0 1 1 5
[101] 0 1 5 1 1 0 0 5 1 10 0 10 5 5 0 1 5 0 10 1 10 10 0 10 0
[126] 5 0 10 5 5 10 10 10 10 10 5 0 5 0 0 0 5 0 10 0 10 5 10 1 0
[151] 10 5 0 10 10 0 5 10 0 0 0 5 5 0 1 5 5 1 5 5 0 10 10 10 1
[176] 0 5 0 0 10 5 5 0 10 10 5 10 0 1 1 1 1 0 0 10 10 1 10 1 5
[201] 1 0 5 10 10 10 1 10 5 5 0 5 10 5 1 5 5 10 5 1 1 0 0 10 0
[226] 0 5 1 1 0 0 1 5 0 10 1 5 0 0 1 10 1 5 0 5 0 5 1 0 5
[251] 1 10 5 0 5 0 1 5 1 1 10 5 10 0 5 0 1 0 0 5 0 5 1 10 10
[276] 0 0 0 10 0 1 5 10 5 5 0 5 5 0 1 5 0 10 1 0 5 1 0 5 0
[301] 1 5 5 1 1 0 10 0 10 10 10 5 10 0 10 5 5 0 5 10 0 10 5 10 5
[326] 0 10 1 0 1 0 1 1 5 1 10 5 0 5 5 5 10 10 10 10 1 10 1 1 10
[351] 5 1 1 1 1 5 0 10 0 10 10 5 10 1 10 5 10 10 10 1 0 0 0 0 10
[376] 5 0 5 0 5 10 1 10 10 1 0 10 10 1 10 1 5 10 0 0 1 0 5 0 0
[401] 5 1 0 10 1 0 10 5 10 5 5 10 0 10 1 0 10 10 1 0 10 0 0 10 10
[426] 5 5 10 1 10 0 5 0 5 1 5 10 5 1 0 5 0 10 0 5 5 5 0 0 10
[451] 5 0 10 5 10 1 5 1 5 0 1 10 5 10 0 1 10 0 0 10 1 1 0 0 5
[476] 0 5 10 5 5 10 5 5 1 5 1 10 1 1 10 0 1 0 1 1 5 1 1 0 10

Example

x5<−factor(x5,levels=c(0:10))
table(x5)

Output

x5
0 1 2 3 4 5 6 7 8 9 10
144 104 0 0 0 129 0 0 0 0 123
Updated on 09-Feb-2021 12:10:29