R Programming to find the column name for row maximum in a data.table object.

R ProgrammingServer Side ProgrammingProgramming

If we have a data.table object and we want to find the column name for row maximum then we can use max.col function.

For Example, if we have a data.table object called DT then we can find the column name for row maximum by using the below command −

DT[,names(.SD)[max.col(.SD,ties.method="first")]]

Example 1

Following snippet creates a sample data frame −

x1<-round(rnorm(20),2)
x2<-round(rnorm(20),2)
x3<-round(rnorm(20),2)
x4<-round(rnorm(20),2)
DT1<-data.table(x1,x2,x3,x4)
DT1

The following dataframe is created

       x1     x2   x3    x4
1:   0.10  -0.77  0.01 -0.28
2:  -0.81  -0.88 -1.25 -1.03
3:  -0.43   1.24  1.27 0.59
4:   1.55  -0.71  0.21 -0.16
5:   1.50  -0.80  0.26  0.32
6:  -0.74  -0.19  0.11 -2.21
7:  -0.12   1.17 -0.08  1.63
8:   1.16  -2.45  0.40 -0.49
9:  -0.42   0.49 -1.43  1.14
10: -0.63   0.79  1.39 -2.28
11:  1.03   0.79  2.10  0.85
12: -1.07  -1.50  2.45  0.05
13:  0.62  -0.25 -0.11  0.50
14: -0.32   1.23 -0.92  1.13
15: -1.14   1.95 -0.36 -0.77
16: -1.15  -0.24  0.59  0.89
17:  2.03  -1.10  1.74 -1.96
18: -0.06   1.47  0.11  1.64
19: -2.13   1.45 -1.64 -0.71
20:  1.37  -0.41 -0.21  0.37

To find the column name for row maximum in DT1 on the above created data frame, add the following code to the above snippet −

x1<-round(rnorm(20),2)
x2<-round(rnorm(20),2)
x3<-round(rnorm(20),2)
x4<-round(rnorm(20),2)
DT1<-data.table(x1,x2,x3,x4)
DT1[,names(.SD)[max.col(.SD,ties.method="first")]]

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "x1" "x1" "x3" "x1" "x1" "x3" "x4" "x1" "x4" "x3" "x3" "x3" "x1" "x2" "x2"
[16] "x4" "x1" "x4" "x2" "x1"

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,5)
y2<-rpois(20,5)
y3<-rpois(20,5)
DT2<-data.table(y1,y2,y3)
DT2

The following dataframe is created

   y1  y2 y3
1:  5  4  4
2:  3  5  4
3:  3  6  6
4:  8  4  7
5:  1 11  8
6:  11 5  7
7:  3  5  7
8:  5  9  8
9:  4  6  4
10: 4  3  2
11: 2  6  4
12: 4  5  3
13: 7  9  7
14: 6 12  3
15: 4 10  5
16: 5  5  3
17: 7  6  4
18: 3  2  3
19: 6  3  3
20: 1  2  5

To find the column name for row maximum in DT2 on the above created data frame, add the following code to the above snippet −

y1<-rpois(20,5)
y2<-rpois(20,5)
y3<-rpois(20,5)
DT2<-data.table(y1,y2,y3)
DT2
DT2[,names(.SD)[max.col(.SD,ties.method="first")]]

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] "y1" "y2" "y2" "y1" "y2" "y1" "y3" "y2" "y2" "y1" "y2" "y2" "y2" "y2" "y2"
[16] "y1" "y1" "y1" "y1" "y3"

Example 3

Following snippet creates a sample data frame −

z1<-rexp(20)
z2<-rexp(20)
z3<-rexp(20)
DT3<-data.table(z1,z2,z3)
DT3

The following dataframe is created

           z1         z2         z3
1:  1.85175520 0.06988659 0.10876221
2:  1.69589804 0.83393956 0.08359516
3:  4.53698884 0.14180505 0.77873494
4:  1.61637232 0.40584249 0.59490613
5:  0.96935637 0.24492793 0.47872486
6:  1.81591399 0.75036377 0.10156744
7:  0.65936883 0.22916612 1.38184152
8:  0.71911018 0.11860198 1.03305886
9:  0.01083114 7.64524696 0.07793300
10: 0.62813099 1.35159142 0.28918956
11: 0.32457686 0.02591335 5.85365604
12: 0.42783663 1.21874776 0.38176176
13: 1.16153548 0.24340230 0.42469609
14: 1.58644074 0.51814087 0.50409917
15: 1.21327299 1.73146461 0.89982450
16: 0.51891446 0.47827326 0.06424925
17: 0.60590047 0.29575678 0.16249550
18: 0.39925252 0.02687194 0.52659081
19: 1.15769384 1.76426512 0.34584972
20: 0.06939622 0.37072691 0.23280432

To find the column name for row maximum in DT3 on the above created data frame, add the following code to the above snippet −

z1<-rexp(20)
z2<-rexp(20)
z3<-rexp(20)
DT3<-data.table(z1,z2,z3)
DT3[,names(.SD)[max.col(.SD,ties.method="first")]]

Output


If you execute all the above given snippets as a single program, it generates the following Output −

[1] "z1" "z1" "z1" "z1" "z1" "z1" "z3" "z3" "z2" "z2" "z3" "z2" "z1" "z1" "z2"
[16] "z1" "z1" "z3" "z2" "z2"
raja
Updated on 02-Nov-2021 10:49:43

Advertisements