![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 median of frequency data in R?
If we have frequency data then we first need to find the total data or complete data by repeating the values up to the frequency corresponding to each value after that we can apply median function on this complete data.
For Example, if we have a data frame called df that contains two columns say X and Frequency then we can find the total data by using the following command −
Total_data<-rep(X,Frequency)
Now the median will be found by using the command as follows −
median(Total_data)
Example 1
Following snippet creates a sample data frame −
x<-rpois(20,20) frequency<-sample(1:10,20,replace=TRUE) df1<-data.frame(x,frequency) df1
The following dataframe is created
x frequency 1 21 9 2 16 8 3 24 7 4 12 6 5 15 10 6 20 9 7 28 1 8 16 8 9 25 7 10 22 10 11 20 7 12 17 3 13 20 2 14 23 2 15 29 7 16 20 7 17 25 9 18 20 3 19 15 10 20 14 2
To find the total data in df1 on the above created data frame, add the following code to the above snippet −
x<-rpois(20,20) frequency<-sample(1:10,20,replace=TRUE) df1<-data.frame(x,frequency) Total_data1<-rep(df1$x,df1$frequency) Total_data1
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 21 21 21 21 21 21 21 21 21 16 16 16 16 16 16 16 16 24 24 24 24 24 24 24 12 [26] 12 12 12 12 12 15 15 15 15 15 15 15 15 15 15 20 20 20 20 20 20 20 20 20 28 [51] 16 16 16 16 16 16 16 16 25 25 25 25 25 25 25 22 22 22 22 22 22 22 22 22 22 [76] 20 20 20 20 20 20 20 17 17 17 20 20 23 23 29 29 29 29 29 29 29 20 20 20 20 [101] 20 20 20 25 25 25 25 25 25 25 25 25 20 20 20 15 15 15 15 15 15 15 15 15 15 [126] 14 14
To find the median of Total_data1 on the above created data frame, add the following code to the above snippet −
x<-rpois(20,20) frequency<-sample(1:10,20,replace=TRUE) df1<-data.frame(x,frequency) Total_data1<-rep(df1$x,df1$frequency) median(Total_data1)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 20
Example 2
Following snippet creates a sample data frame −
y<-rpois(20,20) count<-sample(1:10,20,replace=TRUE) df2<-data.frame(y,count) df2
The following dataframe is created
y count 1 21 10 2 17 10 3 28 4 4 24 5 5 15 3 6 25 7 7 23 6 8 15 7 9 21 7 10 21 7 11 26 5 12 19 10 13 15 9 14 31 9 15 20 4 16 19 6 17 21 9 18 19 6 19 25 7 20 21 9
To find total data in df2 on the above created data frame, add the following code to the above snippet −
y<-rpois(20,20) count<-sample(1:10,20,replace=TRUE) df2<-data.frame(y,count) Total_data2<-rep(df2$y,df2$count) Total_data2
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 21 21 21 21 21 21 21 21 21 21 17 17 17 17 17 17 17 17 17 17 28 28 28 28 24 [26] 24 24 24 24 15 15 15 25 25 25 25 25 25 25 23 23 23 23 23 23 15 15 15 15 15 [51] 15 15 21 21 21 21 21 21 21 21 21 21 21 21 21 21 26 26 26 26 26 19 19 19 19 [76] 19 19 19 19 19 19 15 15 15 15 15 15 15 15 15 31 31 31 31 31 31 31 31 31 20 [101] 20 20 20 19 19 19 19 19 19 21 21 21 21 21 21 21 21 21 19 19 19 19 19 19 25 [126] 25 25 25 25 25 25 21 21 21 21 21 21 21 21 21
To find the median of Total_data2 on the above created data frame, add the following code to the above snippet −
y<-rpois(20,20) count<-sample(1:10,20,replace=TRUE) df2<-data.frame(y,count) Total_data2<-rep(df2$y,df2$count) median(Total_data2)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 21
Example 3
Following snippet creates a sample data frame −
z<-sample(1:2,20,replace=TRUE) count<-sample(1:10,20,replace=TRUE) df3<-data.frame(z,count) df3
The following dataframe is created
z count 1 2 3 2 1 1 3 1 6 4 1 7 5 1 7 6 1 4 7 2 9 8 1 7 9 2 10 10 2 6 11 1 4 12 1 6 13 2 6 14 1 7 15 1 9 16 1 4 17 2 4 18 2 4 19 2 8 20 2 2
To find total data in df3 on the above created data frame, add the following code to the above snippet −
z<-sample(1:2,20,replace=TRUE) count<-sample(1:10,20,replace=TRUE) df3<-data.frame(z,count) Total_data3<-rep(df3$z,df3$count) Total_data3
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 [38] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 2 [75] 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 [112] 2 2 2
To find the median of Total_data3 on the above created data frame, add the following code to the above snippet −
z<-sample(1:2,20,replace=TRUE) count<-sample(1:10,20,replace=TRUE) df3<-data.frame(z,count) Total_data3<-rep(df3$z,df3$count) median(Total_data3)
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] 1