
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Column Minimum for Categorical Columns in R Data Frame
To find the column minimum 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 minimum if some columns are categorical.
Example
Create the data frame
Let’s create a data frame as shown below −
Group<-sample(c("I","II","III","IV"),25,replace=TRUE) Num1<-sample(1:50,25) Num2<-sample(1:50,25) df1<-data.frame(Group,Num1,Num2) df1
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Group Num1 Num2 1 IV 45 19 2 III 37 5 3 IV 21 27 4 III 16 26 5 III 34 48 6 I 3 11 7 II 49 10 8 I 24 8 9 I 26 25 10 II 20 49 11 I 29 29 12 II 8 12 13 I 7 31 14 III 44 38 15 III 2 20 16 III 23 37 17 I 15 15 18 I 42 45 19 I 11 47 20 IV 6 40 21 II 41 3 22 II 48 14 23 III 31 24 24 IV 13 17 25 II 5 41
Find the column minimum if some columns are categorical
Using numcolwise function from plyr package to find the column minimum of numerical columns in the data frame df1 −
Group<-sample(c(“I”,”II”,”III”,”IV”),25,replace=TRUE) Num1<-sample(1:50,25) Num2<-sample(1:50,25) df1<-data.frame(Group,Num1,Num2) library(plyr) numcolwise(min)(df1)
Output
Num1 Num2 1 2 3
Example 2
Create the data frame
Let’s create a data frame as shown below −
Categories<-sample(c("First","Second","Third"),25,replace=TRUE) Score<-sample(1:10,25,replace=TRUE) Price<-sample(1:5,25,replace=TRUE) df2<-data.frame(Categories,Score,Price) df2
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Categories Score Price 1 Third 8 1 2 First 8 1 3 Second 8 3 4 Second 8 2 5 Third 1 1 6 Second 1 1 7 First 5 3 8 Third 5 3 9 First 4 1 10 Second 6 3 11 Third 6 3 12 Third 2 4 13 Second 7 4 14 First 5 3 15 Second 1 4 16 Third 1 5 17 Third 7 4 18 Third 10 4 19 Third 1 3 20 Second 4 3 21 Second 3 1 22 Second 10 4 23 First 3 5 24 First 1 4 25 Second 6 4
Find the column minimum if some columns are categorical
Using numcolwise function from plyr package to find the column minimum of numerical columns in the data frame df2 −
Categories<-sample(c("First","Second","Third"),25,replace=TRUE) Score<-sample(1:10,25,replace=TRUE) Price<-sample(1:5,25,replace=TRUE) df2<-data.frame(Categories,Score,Price) library(plyr) numcolwise(min)(df2)
Output
Score Price 1 1 1