How to count the number of occurrences of all unique values in an R data frame?


A data frame in R can have infinite number of unique values and it can also contain many repeated values. Therefore, finding the number of all unique values in the data frame can help us to understand the diversity in the data but this most done in situations where we expect to have repeated elements otherwise it would not make sense. To count the number of occurrences of all unique values, we can use table function along with the unlist as shown in the below examples.

Consider the below data frame −

Example

 Live Demo

x1<-sample(LETTERS[1:5],20,replace=TRUE)
x2<-sample(LETTERS[1:5],20,replace=TRUE)
x3<-sample(LETTERS[1:5],20,replace=TRUE)
x4<-sample(LETTERS[1:5],20,replace=TRUE)
x5<-sample(LETTERS[1:5],20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4,x5)
df1

Output

  x1 x2 x3 x4 x5
1  B  E  D  E  E
2  E  A  C  E  E
3  C  A  D  A  D
4  C  C  A  D  C
5  D  D  A  C  B
6  C  C  E  E  B
7  B  B  C  B  A
8  A  E  B  C  B
9  E  D  E  B  E
10 C  B  A  C  A
11 C  C  C  B  D
12 A  B  C  A  A
13 C  D  D  C  C
14 E  C  E  D  C
15 A  A  B  D  E
16 E  D  E  A  E
17 C  C  A  D  E
18 C  C  E  C  D
19 B  B  A  E  B
20 D  B  D  A  B

Finding the number of unique values in the data frame df1 −

Example

table(unlist(df1))

Output

A   B  C  D  E 
18 27 22 17 16

Let’s have a look at another example −

Example

 Live Demo

y1<-sample(0:2,20,replace=TRUE)
y2<-sample(0:2,20,replace=TRUE)
y3<-sample(0:2,20,replace=TRUE)
y4<-sample(0:2,20,replace=TRUE)
df2<-data.frame(y1,y2,y3,y4)
df2

Output

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

Finding the number of unique values in the data frame df2 −

Example

table(unlist(df2))

Output

 0  1  2
32 26 22

Updated on: 19-Oct-2020

619 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements