Find the column name that contains value greater than a desired value in each row of an R data frame.


To find the column name that contains value greater than a desired value in each row of an R data frame, we can use apply function along with lapply function.

For Example, if we have a data frame called df and we want to extract column names for each row having values greater than 5 then we can use the command given below −

lapply(apply(df,1, function(x) which(x5)),names)

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,5)
x2<-rpois(20,2)
x3<-rpois(20,1)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created

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

To find column names that have value greater than 1 for each row on the above created data frame, add the following code to the above snippet −

x1<-rpois(20,5)
x2<-rpois(20,2)
x3<-rpois(20,1)
df1<-data.frame(x1,x2,x3)
lapply(apply(df1,1, function(x) which(x1)),names)

Output

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

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

Example 2

Following snippet creates a sample data frame −

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

The following dataframe is created

     y1   y2   y3   y4
 1 -1.3 -0.7 -0.8  0.1
 2 -0.2 -0.2  0.7 -0.7
 3 0.8  -0.5  0.2 -0.1
 4 -0.6  0.4  0.3 -0.8
 5 -0.3  1.3  0.4  1.3
 6 -0.3  1.0 -0.1 -1.2
 7 -0.2  0.6 -2.1  0.5
 8  1.0  1.4  0.2 -1.7
 9  1.1  0.4  0.6  1.2
10 -1.0 -0.8  1.7  0.2
11 -0.6 -1.0  0.1 -0.2
12  0.6  0.3  0.0 -0.2
13 -0.3  0.6 -0.4 -1.0
14  0.9  0.0 -0.3  1.7
15 -0.2  3.3  0.7 -0.7
16 -0.6  0.1 -0.7 -0.6
17 -0.2  0.7 -0.6  1.9
18  1.0 -0.5 -0.8  0.6
19 -0.9  0.1 -0.6 -0.5
20 -1.5 -1.0 -0.6  1.2

To find column names that have value greater than 0.5 for each row on the above created data frame, add the following code to the above snippet −

y1<-round(rnorm(20),1)
y2<-round(rnorm(20),1)
y3<-round(rnorm(20),1)
y4<-round(rnorm(20),1)
df2<-data.frame(y1,y2,y3,y4)
lapply(apply(df2,1, function(x) which(x0.5)),names)

Output

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

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

Updated on: 09-Nov-2021

855 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements