# How to find the column number of minimum values in each row for a data frame in R?

To find the column number of minimum values in each row for a data frame, we can use apply function but if we want to return the output in tabular form then matrix function should be used. For example, if we have a data frame df then our problem can be solved by using the code: as.matrix(apply(df,1,which.min)).

## Example

Consider the below data frame:

Live Demo

> set.seed(37)
> x1<-rpois(20,1)
> x2<-rpois(20,3)
> x3<-rpois(20,5)
> x4<-rpois(20,8)
> x5<-rpois(20,3)
> df1<-data.frame(x1,x2,x3,x4,x5)
> df1

## Output

x1 x2 x3 x4 x5
1 1 2 4 9 3
2 0 5 8 10 4
3 1 3 8 6 1
4 1 5 5 8 1
5 1 6 7 10 1
6 2 2 3 8 2
7 1 5 1 7 6
8 0 5 3 11 1
9 0 3 7 9 0
10 0 1 7 4 1
11 1 0 4 8 4
12 1 1 6 8 6
13 1 0 5 7 3
14 0 3 4 6 1
15 2 0 5 8 5
16 2 1 1 7 6
17 2 4 1 7 1
18 0 0 4 11 4
19 1 2 6 10 2
20 2 4 14 6 5

Finding the column number of minimum values for each row:

## Example

> as.matrix(apply(df1,1,which.min))

## Output

   [,1]
[1,] 1
[2,] 1
[3,] 1
[4,] 1
[5,] 1
[6,] 1
[7,] 1
[8,] 1
[9,] 1
[10,] 1
[11,] 2
[12,] 1
[13,] 2
[14,] 1
[15,] 2
[16,] 2
[17,] 3
[18,] 1
[19,] 1
[20,] 1

Let’s have a look at another example:

## Example

Live Demo

> y1<-rnorm(20)
> y2<-rnorm(20)
> y3<-rnorm(20)
> y4<-rnorm(20)
> y5<-rnorm(20)
> y6<-rnorm(20)
> df2<-data.frame(y1,y2,y3,y4,y5,y6)
> df2

## Output

      y1         y2           y3           y4         y5        y6
1 0.60626817 -0.91135701 -1.54130303 -0.179728310 -0.11247788 -1.6229564
2 0.19393903 -1.07692650 -0.40583883 0.792882282 -0.24669868 1.2778953
3 1.34801696 1.13319435 -1.52777490 -0.271678353 1.73391148 -0.1999457
4 1.47746318 -2.02080815 0.91405420 0.187589191 -0.13964786 0.5949776
5 0.13006357 -0.12006636 0.07832255 0.245816917 -0.02587037 1.9145620
6 1.15608052 0.78724570 0.21028660 -0.800353797 0.17037962 2.0228097
7 -1.66547295 0.15968512 -0.41067917 1.163657693 -0.80061836 -0.2003318
8 -0.99577105 -0.71316794 0.07191634 -1.587010612 -1.22748045 0.3979554
9 1.52424854 1.08072776 -0.47782945 0.033819730 -1.69111494 -0.3684224
10 -0.64126977 1.68534888 2.39972590 -0.679471637 1.10681706 0.6740395
11 0.17454722 -0.91772017 1.55791731 0.730534135 0.43243721 -0.7367532
12 -0.35585875 -0.06142613 1.59953661 0.336857456 -0.94200575 -1.8501486
13 -1.68668979 -1.07762950 -0.27908596 -0.537678939 0.04321625 0.6898380
14 1.87904655 -1.60792421 1.24671576 1.117086006 -0.58952288 1.6803463
15 -1.44386867 0.78773146 2.74858228 0.132986370 0.77109554 -0.6953031
16 1.12119646 -1.17683926 -0.59541289 0.911623304 1.34332211 -0.8758561
17 -0.05182865 -0.65743777 0.79942253 1.915909217 -2.10284503 0.4708889
18 1.02779621 0.35756128 1.20166249 0.683214669 -0.06237268 -0.7683743
19 0.39594576 -0.08108464 -0.18974225 -0.008500698 0.38907174 -1.2654953
20 -0.57351319 -2.00570145 1.78322840 -0.412478686 0.35721591 -1.3844073

Finding the column number of minimum values for each row:

## Example

> as.matrix(apply(df2,1,which.min))

## Output

    [,1]
[1,]  6
[2,]  2
[3,]  3
[4,]  2
[5,]  2
[6,]  4
[7,]  1
[8,]  4
[9,]  5
[10,] 4
[11,] 2
[12,] 6
[13,] 1
[14,] 2
[15,] 1
[16,] 2
[17,] 5
[18,] 6
[19,] 6
[20,] 2

Updated on: 19-Nov-2020

502 Views