Find the column name with the largest value for each row in an R data frame.

R ProgrammingServer Side ProgrammingProgramming

To find the column name that has the largest value for each row in an R data frame, we can use colnames function along with apply function.

For Example, if we have a data frame called df then we can find column name that has the largest value for each row by using the command as follows −

df$Largest_Column<-colnames(df)[apply(df,1,which.max)]

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created

             x1         x2          x3
1  -0.305888032  1.42530072 -0.60397460
2   0.077412581  1.33102088  1.09897001
3  -0.001797155  1.85365113  0.59881492
4  -0.235863387 -0.11476965 -0.23914040
5   0.641954539 -0.80069293  1.78915326
6   1.662750089 -0.48168001 -1.63141513
7   1.393413983 -0.21044222 -0.36966594
8   0.387820650  0.04998259 -0.88707049
9  -0.982245543 -1.04089646  1.51510464
10  1.540251727 -0.24360161 -0.72272136
11  0.871043177 -1.61258877 -0.08300941
12  0.894436819  1.22285505  0.25353571
13 -0.706468609  0.37879788  1.09617879
14  1.366866702 -2.36429211  0.47667869
15  0.827015705 -0.29348558  2.57175974
16 -0.709173752 -0.68338183 -0.15060505
17  0.464121383 -0.41577526 -1.52947993
18 -0.322493725  0.46212973  1.38418790
19  0.588932732 -1.98841476  0.43082069
20 -0.775650742 -0.45247281  0.62378543

To find the column name that has the largest value for each row in df1 on the above created data frame, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20)
x3<-rnorm(20)
df1<-data.frame(x1,x2,x3)
df1$Largest_Column<-colnames(df1)[apply(df1,1,which.max)]
df1

Output

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

             x1          x2          x3 Largest_Column
1  -0.305888032  1.42530072 -0.60397460            x2
2   0.077412581  1.33102088  1.09897001            x2
3  -0.001797155  1.85365113  0.59881492            x2
4  -0.235863387 -0.11476965 -0.23914040            x2
5   0.641954539 -0.80069293  1.78915326            x3
6   1.662750089 -0.48168001 -1.63141513            x1
7   1.393413983 -0.21044222 -0.36966594            x1
8   0.387820650  0.04998259 -0.88707049            x1
9  -0.982245543 -1.04089646  1.51510464            x3
10  1.540251727 -0.24360161 -0.72272136            x1
11  0.871043177 -1.61258877 -0.08300941            x1
12  0.894436819  1.22285505  0.25353571            x2
13 -0.706468609  0.37879788  1.09617879            x3
14  1.366866702 -2.36429211  0.47667869            x1
15  0.827015705 -0.29348558  2.57175974            x3
16 -0.709173752 -0.68338183 -0.15060505            x3
17  0.464121383 -0.41577526 -1.52947993            x1
18 -0.322493725  0.46212973  1.38418790            x3
19  0.588932732 -1.98841476  0.43082069            x1
20 -0.775650742 -0.45247281  0.62378543            x3

Example 2

Following snippet creates a sample data frame −

y1<-rpois(20,5)
y2<-rpois(20,5)
y3<-rpois(20,5)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
df2

The following dataframe is created

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

To find the column name that has the largest value for each row in df2 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)
y4<-rpois(20,5)
df2<-data.frame(y1,y2,y3,y4)
df2$Largest_Col<-colnames(df2)[apply(df2,1,which.max)]
df2

Output

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

   y1 y2 y3 y4 Largest_Col
1   4  5  4  8          y4
2   6  6  8  5          y3
3   4  7  5  4          y2
4   8  2  7  7          y1
5   3  6  5  0          y2
6   4  5  4  7          y4
7   6  1  2  9          y4
8   5  4  3  5          y1
9   9  5  6  5          y1
10  8  3  7  9          y4
11  5 14  7  5          y2
12  4  7  4  4          y2
13  9  3  1  2          y1
14  5  8  9  4          y3
15  2  7  2  5          y2
16  4  3  3  5          y4
17  4  6  4  4          y2
18  7  6  4  4          y1
19 12  6  8  4          y1
20  6  6  4  5          y1
raja
Published on 01-Nov-2021 06:22:24
Advertisements