- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the frequency of unique values for each column in an R data frame.
To find the frequency of unique values for each column in an R data frame, we can use apply function with table function.
For Example, if we have a data frame called df then we can find the frequency of unique values for each column in df by using the below mentioned command −
apply(df,2,table)
Example 1
Following snippet creates a sample data frame −
x1<-sample(c(2,5,7,4),20,replace=TRUE) x2<-sample(c(2,5,7,4),20,replace=TRUE) x3<-sample(c(2,5,7,4),20,replace=TRUE) df1<-data.frame(x1,x2,x3) df1
The following dataframe is created
x1 x2 x3 1 2 5 7 2 5 4 5 3 5 5 5 4 5 4 4 5 5 5 2 6 5 7 7 7 7 7 2 8 2 2 2 9 4 4 7 10 2 5 5 11 2 4 4 12 5 7 5 13 2 5 4 14 2 5 4 15 5 5 7 16 5 5 4 17 5 7 5 18 2 4 4 19 7 7 5 20 5 5 7
To find the frequency of unique values for each column in df1 on the above created data frame, add the following code to the above snippet −
x1<-sample(c(2,5,7,4),20,replace=TRUE) x2<-sample(c(2,5,7,4),20,replace=TRUE) x3<-sample(c(2,5,7,4),20,replace=TRUE) df1<-data.frame(x1,x2,x3) apply(df1,2,table)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
x1 x2 x3 2 7 1 3 4 1 5 6 5 10 9 6 7 2 5 5
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 3 1 1 2 4 0 1 3 1 2 4 4 0 3 1 5 2 0 1 6 2 0 0 7 2 1 1 8 3 2 0 9 2 0 0 10 1 0 2 11 1 3 0 12 2 1 0 13 1 1 2 14 0 3 2 15 0 2 2 16 0 0 1 17 2 1 2 18 4 2 1 19 0 1 4 20 0 0 3
To find the frequency of unique values for each column 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) apply(df2,2,table)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
$y1 0 1 2 3 4 6 4 6 2 2 $y2 0 1 2 3 7 6 4 3 $y3 0 1 2 3 4 5 7 5 1 2
Example 3
Following snippet creates a sample data frame −
z1<-sample(0:4,20,replace=TRUE) z2<-sample(0:4,20,replace=TRUE) z3<-sample(0:4,20,replace=TRUE) df3<-data.frame(z1,z2,z3) df3
The following dataframe is created
z1 z2 z3 1 2 2 1 2 4 1 3 3 1 3 4 4 4 4 1 5 0 0 3 6 0 4 3 7 3 1 3 8 4 1 1 9 3 4 3 10 3 4 1 11 4 4 0 12 2 4 0 13 3 1 4 14 3 3 1 15 4 3 1 16 0 2 3 17 3 3 2 18 3 4 2 19 0 3 0 20 4 4 4
To find the frequency of unique values for each column in df3 on the above created data frame, add the following code to the above snippet −
z1<-sample(0:4,20,replace=TRUE) z2<-sample(0:4,20,replace=TRUE) z3<-sample(0:4,20,replace=TRUE) df3<-data.frame(z1,z2,z3) apply(df3,2,table)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
z1 z2 z3 0 4 1 3 1 1 4 6 2 2 2 2 3 7 5 6 4 6 8 3