- Trending Categories
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
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
- Related Articles
- How to find the frequency of particular value in rows of an R data frame?
- Find the frequency of unique values for each column in an R data frame.
- How to find the ID wise frequency in an R data frame?
- How to find the row-wise frequency of zeros in an R data frame?
- How to find the maximum value in an R data frame?
- How to find the frequency of NA values per row in an R data frame?
- How to find the maximum of each row in an R data frame?
- How to divide each value in an R data frame by 100?
- How to find the frequency table for factor columns in an R data frame?
- How to create a data frame of the maximum value for each group in an R data frame using dplyr?
- Find the frequency of unique values and missing values for each column in an R data frame.
- How to find the frequency of continuously repeated string values in an R data frame column?
- How to find the percentage of each category in an R data frame column?
- How to find the count of each category in an R data frame column?
- How to find the frequency based on intervals in R data frame?
