- 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 column means if some columns are categorical in R data frame?
To find the column means if some columns are categorical in R data frame, we can follow the below steps −
First of all, create a data frame.
Then, use numcolwise function from plyr package to find the column means ifsome columns are categorical.
Example 1
Create the data frame
Let’s create a data frame as shown below −
Grp<-sample(c("Male","Female"),25,replace=TRUE) Score<-sample(1:50,25) Frequency<-sample(1:5,25,replace=TRUE) df1<-data.frame(Grp,Score,Frequency) df1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Grp Score Frequency 1 Male 22 2 2 Male 10 2 3 Female 38 4 4 Female 13 2 5 Male 24 5 6 Male 39 2 7 Male 23 5 8 Female 42 3 9 Female 15 4 10 Female 49 4 11 Male 18 2 12 Female 30 5 13 Male 17 2 14 Male 4 3 15 Male 34 2 16 Female 3 5 17 Male 35 2 18 Female 31 3 19 Female 43 5 20 Female 9 4 21 Female 12 4 22 Female 8 4 23 Male 29 2 24 Male 46 3 25 Male 33 2
Find the column means if some columns are categorical
Using numcolwise function from plyr package to find the column means of numerical columns in the data frame df1 −
Grp<-sample(c("Male","Female"),25,replace=TRUE) Score<-sample(1:50,25) Frequency-<sample(1:5,25,replace=TRUE) df1<-data.frame(Grp,Score,Frequency) library(plyr) numcolwise(mean)(df1)
Output
Score Frequency 1 25.08 3.24
Example 2
Create the data frame
Let’s create a data frame as shown below −
factor<-sample(LETTERS[1:4],25,replace=TRUE) v1<-rpois(25,2) v2<-rpois(25,5) df2<-data.frame(factor,v1,v2) df2
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
factor v1 v2 1 C 3 7 2 C 2 5 3 C 2 5 4 A 3 7 5 C 4 8 6 B 0 6 7 C 3 6 8 D 3 4 9 D 2 4 10 A 6 10 11 A 2 4 12 A 2 5 13 B 3 6 14 C 2 7 15 A 2 4 16 C 0 6 17 C 3 7 18 B 1 7 19 C 1 6 20 B 2 3 21 A 1 8 22 C 1 1 23 A 3 7 24 A 4 7 25 B 1 7
Find the column means if some columns are categorical
Using numcolwise function from plyr package to find the column means of numerical columns in the data frame df2 −
factor<-sample(LETTERS[1:4],25,replace=TRUE) v1<-rpois(25,2) v2<-rpois(25,5) df2<-data.frame(factor,v1,v2) library(plyr) numcolwise(mean)(df2)
Output
v1 v2 1 2.24 5.88
- Related Articles
- How to find the column variance if some columns are categorical in R data frame?
- How to find the column median if some columns are categorical in R data frame?
- How to find the column minimum if some columns are categorical in R data frame?
- How to find the column maximum if some columns are categorical in R data frame?
- How to find the column totals if some columns are categorical in R data frame?
- How to find the column standard deviation if some columns are categorical in R data frame?
- How to standardize columns if some columns are categorical in R data frame?
- How to find the range of columns if some columns are categorical in R data frame?
- How to find the cumulative sum of columns if some columns are categorical in R data frame?
- How to find the position of maximum of each numerical column if some columns are categorical in R data frame?
- How to find the position of minimum of each numerical column if some columns are categorical in R data frame?
- How to round each value in columns if some columns are categorical in R data frame?
- How to find the cosine of each value in columns if some columns are categorical in R data frame?
- How to find the log of each value in columns if some columns are categorical in R data frame?
- How to find the exponent of each value in columns if some columns are categorical in R data frame?
