Find the column name of least value in each row of an R dataframe.

R ProgrammingServer Side ProgrammingProgramming

To find the column name that has the least 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 least value for each row by using the command mentioned below −

df$Least_Column<-colnames(df)[apply(df,1,which.min)]

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,10)
x2<-rpois(20,10)
x3<-rpois(20,10)
x4<-rpois(20,10)
df1<-data.frame(x1,x2,x3,x4)
df1

The following dataframe is created

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

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

x1<-rpois(20,10)
x2<-rpois(20,10)
x3<-rpois(20,10)
x4<-rpois(20,10)
df1<-data.frame(x1,x2,x3,x4)
df1$Smallest_Col<-colnames(df1)[apply(df1,1,which.min)]
df1

Output

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

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

Example 2

Following snippet creates a sample data frame −

y1<-round(rnorm(20),2)
y2<-round(rnorm(20),2)
y3<-round(rnorm(20),2)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

      y1    y2 y3
 1 -0.33  0.19 -0.18
 2 -1.41 -0.42 -0.06
 3 -0.48 -0.62 -0.51
 4 -0.27  0.68  0.38
 5  1.00  1.04  1.31
 6 -0.29  0.04 -1.23
 7  0.65 -1.47 -1.11
 8  0.33  0.14  0.80
 9  1.29  0.20  1.14
10 -0.26  0.10  0.64
11  0.42 -0.17  0.64
12 -0.31  1.53 -0.41
13  0.21 -0.87 -1.03
14  0.85  1.82 -1.35
15  0.80  0.89  0.45
16  0.65  1.08  0.08
17 -0.05 -1.16  0.35
18 -0.91 -0.19 -0.93
19  0.14 -1.30 -0.91
20 -0.03  2.02  1.41

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

y1<-round(rnorm(20),2)
y2<-round(rnorm(20),2)
y3<-round(rnorm(20),2)
df2<-data.frame(y1,y2,y3)
df2$Smallest_Col<-colnames(df2)[apply(df2,1,which.min)]
df2

Output

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

      y1    y2  y3 Smallest_Col
 1 -0.33  0.19 -0.18  y1
 2 -1.41 -0.42 -0.06  y1
 3 -0.48 -0.62 -0.51  y2
 4 -0.27  0.68  0.38  y1
 5  1.00  1.04  1.31  y1
 6 -0.29  0.04 -1.23  y3
 7  0.65 -1.47 -1.11  y2
 8  0.33  0.14  0.80  y2
 9  1.29  0.20  1.14  y2
10 -0.26  0.10  0.64  y1
11  0.42 -0.17  0.64  y2
12 -0.31  1.53 -0.41  y3
13  0.21 -0.87 -1.03  y3
14  0.85  1.82 -1.35  y3
15  0.80  0.89  0.45  y3
16  0.65  1.08  0.08  y3
17 -0.05 -1.16 0.35   y2
18 -0.91 -0.19 -0.93  y3
19  0.14 -1.30 -0.91  y2
20 -0.03  2.02 1.41   y1
raja
Published on 05-Nov-2021 08:13:04

Advertisements