How to find the position of minimum of each numerical column if some columns are categorical in R data frame?



To find the position of minimum of each numerical column 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 minimum of each numerical column if some columns are categorical.

Example 1

Create the data frame

Let’s create a data frame as shown below −

factor1<-sample(c("Super","Lower","Medium"),25,replace=TRUE)
factor2<-sample(c("I","II","III"),25,replace=TRUE)
v1<-rnorm(25)
v2<-rnorm(25)
df1<-data.frame(factor1,factor2,v1,v2)
df1

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

    factor1 factor2    v1           v2
1  Medium     III  -2.14379228  -0.75943453
2  Super       I   -1.47939801   1.59666611
3  Lower      III   1.23368296  -1.38936623
4  Lower      III   0.20952920  -0.04218399
5  Medium     III   0.05884382  -0.84164117
6  Super       II   1.96347308   0.88664585
7  Lower       II   0.31493547  -1.27612944
8  Super      III  -1.51226987   1.29224755
9  Medium     III   1.52328734   1.32171715
10 Medium       I  -0.16912317   0.46726813
11 Medium       I   0.24834413  -0.70308350
12 Super        I  -1.16519280  -0.04263700
13 Medium     III   0.17775948  -1.24434106
14 Super       II   0.60504839  -0.50105846
15 Medium      II  -1.65936651  -1.57653244
16 Medium     III   0.96991787  -0.62284764
17 Medium      I    0.67194953   0.72451047
18 Lower      III   0.73096741  -0.93229961
19 Super      III  -0.75207261  -1.34829579
20 Lower        I  -0.19563701   0.06724962
21 Lower        I   0.15769044   1.08397200
22 Lower      III  -1.01078566   0.34309269
23 Medium       I  -0.16170496   0.29763892
24 Lower      III   2.54521045   0.14029670
25 Lower        I   0.96949903  -0.43465951

Find the minimum of each column if some columns are categorical

Using numcolwise function from plyr package to find the minimum of each numerical column if some columns are categorical in data frame df1 −

factor1<-sample(c("Super","Lower","Medium"),25,replace=TRUE)
factor2<-sample(c("I","II","III"),25,replace=TRUE)
v1<-rnorm(25)
v2<-rnorm(25)
df1<-data.frame(factor1,factor2,v1,v2)
library(plyr)
numcolwise(which.min)(df1)

Output

  v1 v2
1 1  15

Example 2

Create the data frame

Let’s create a data frame as shown below −

Level<-sample(c("low","medium","high"),25,replace=TRUE)
Group<-sample(c("first","second"),25,replace=TRUE)
DV1<-rpois(25,5)
DV2<-rpois(25,10)
df2<-data.frame(Level,Group,DV1,DV2)
df2

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

    Level Group   DV1 DV2
1  medium second  6   12
2  medium second  5    8
3  high   first   8    7
4  high   second  4   17
5  medium second  4    7
6  high   second  7   11
7  high   second  6   12
8  medium second  8    6
9  high   first   5   10
10 medium first   3    6
11 low    first   5   12
12 medium second  9   15
13 low    second  7   14
14 low    first   5   11
15 low    first   7    7
16 high   second  5   11
17 high   second 10   13
18 high   first   7   10
19 high   first   2   11
20 low    second  8    9
21 medium first   4    5
22 medium first  11   13
23 low    second  2   12
24 medium first   7   10
25 high   first   5   10

Find the minimum of each column if some columns are categorical

Using numcolwise function from plyr package to find the minimum of each numerical column if some columns are categorical in data frame df2 −

Level<-sample(c("low","medium","high"),25,replace=TRUE)
Group<-sample(c("first","second"),25,replace=TRUE)
DV1<-rpois(25,5)
DV2<-rpois(25,10)
df2<-data.frame(Level,Group,DV1,DV2)
library(plyr)
numcolwise(which.min)(df2)

Output

  DV1 DV2
1 19  21

Advertisements