How to find the column maximum if some columns are categorical in R data frame?

R ProgrammingServer Side ProgrammingProgramming

To find the column maximum 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 maximum if some columns are categorical.

Example 1

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  III   43   36
2   I    5    22
3  IV    3    23
4  II   40    40
5  IV   47    33
6  II   12     3
7  II   31    21
8  IV   34    20
9  I    41    44
10 II    7     8
11 II   35    45
12 III  50    25
13 III  19    16
14 III  11    34
15 I     1    28
16 III  21    31
17 IV    9    47
18 III  15     9
19 I    23    11
20 III  45    24
21 I    37    26
22 I    13    32
23 I    27    30
24 II    2    10
25 I    48    12

Find the column maximum if some columns are categorical

Using numcolwise function from plyr package to find the column maximum 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(max)(df1)

Output

  Num1 Num2
1  50   47

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      6      1
2  Second     7      1
3  Second     5      5
4  First      8      3
5  Third      5      2
6  First      9      2
7  Third      6      5
8  First      9      4
9  First      4      1
10 Second     4      5
11 Second     2      1
12 First      9      3
13 Second     4      3
14 First      4      2
15 Third      5      4
16 Third      2      5
17 First      1      4
18 First      2      1
19 Third      2      5
20 Second     1      1
21 Third      7      1
22 Second     4      2
23 First     10      1
24 Third      6      4
25 First      8      4

Find the column maximum if some columns are categorical

Using numcolwise function from plyr package to find the column maximum 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(max)(df2)

Output

  Score Price
1  10    5
raja
Published on 09-Nov-2021 07:15:32
Advertisements