How to find the frequency of each value in an R data frame?


To find the frequency of each value in an R data frame, we can use table function along with unlist function.

For Example, if we have a data frame called df and we want to find the frequency of each value in df then we can use the below command −

table(unlist(df))

Example 1

Following snippet creates a sample data frame −

x1<-sample(0:9,20,replace=TRUE)
x2<-sample(0:9,20,replace=TRUE)
x3<-sample(0:9,20,replace=TRUE)
x4<-sample(0:9,20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4)
df1

The following dataframe is created

  x1 x2 x3 x4
 1 0 2  8  0
 2 6 6  4  2
 3 8 2  5  8
 4 0 3  8  5
 5 5 1  2  5
 6 8 6  2  9
 7 9 3  4  2
 8 1 7  3  4
 9 4 0  3  6
10 3 9  4  6
11 9 7  9  2
12 4 1  8  7
13 8 6  9  7
14 0 3  5  7
15 6 7  2  6
16 4 1  5  4
17 6 4  0  4
18 9 3  6  3
19 1 6  5  1
20 8 6  0  5

To find the frequency of each value in df1 on the above created data frame, add the following code to the above snippet −

x1<-sample(0:9,20,replace=TRUE)
x2<-sample(0:9,20,replace=TRUE)
x3<-sample(0:9,20,replace=TRUE)
x4<-sample(0:9,20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4)
table(unlist(df1))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

0 1 2 3  4 5  6 7 8 9
7 6 8 8 10 8 12 6 8 7

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,1)
y2<-rpois(20,1)
y3<-rpois(20,1)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

  y1 y2 y3
 1 1 1  1
 2 0 0  0
 3 1 5  0
 4 1 1  2
 5 0 2  0
 6 0 2  1
 7 0 2  0
 8 0 0  2
 9 0 0  1
10 2 1  0
11 0 0  1
12 2 1  0
13 1 1  2
14 0 0  0
15 1 2  1
16 3 2  0
17 0 0  1
18 1 0  0
19 1 1  0
20 1 1  0

To find the frequency of each value in df2 on the above created data frame, add the
following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,1)
y3<-rpois(20,1)
df2<-data.frame(y1,y2,y3)
table(unlist(df2))

Output

If you execute all the above given snippets as a single program, it generates the
following Output −

 0  1  2 3 5
27 21 10 1 1

Example 3

Following snippet creates a sample data frame −

z1<-round(rnorm(20),0)
z2<-round(rnorm(20),0)
z3<-round(rnorm(20),0)
df3<-data.frame(z1,z2,z3)
df3

The following dataframe is created

  z1  z2 z3
 1  1  1  0
 2 -1 -2 -1
 3  0  1  0
 4  0  0  0
 5 -1 -2  0
 6 -1 -1 -1
 7  0  1  0
 8  0 -1  0
 9 -1 -1  1
10  0  0  1
11 -2 -1  1
12  1 -1  0
13 -2  0  0
14 -2 -1  1
15  1  1  0
16  2  0  0
17  0  0  1
18  0  1  0
19  0  1  0
20  1  1 -2

To find the frequency of each value in df3 on the above created data frame, add the
following code to the above snippet −

z1<-round(rnorm(20),0)
z2<-round(rnorm(20),0)
z3<-round(rnorm(20),0)
df3<-data.frame(z1,z2,z3)
table(unlist(df3))

Output

If you execute all the above given snippets as a single program, it generates the
following Output −

-2 -1  0  1 2
 6 12 25 16 1

Updated on: 08-Nov-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements