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

R ProgrammingServer Side ProgrammingProgramming

#### Artificial Intelligence : The Future Of Programming

15 Lectures 54 mins

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

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 −

[]
 "x3"
[]
 "x1" "x2" "x3"
[]
 "x1" "x2"
[]
 "x1" "x3"
[]
 "x1" "x2" "x3"
[]
 "x1" "x2" "x3"
[]
 "x1" "x2"
[]
 "x1" "x2"
[]
 "x1" "x3"
[]
 "x1" "x2" "x3"
[]
 "x1"
[]
 "x1" "x2"
[]
 "x1" "x2"
[]
 "x1"
[]
 "x1" "x2"
[]
 "x1" "x3"
[]
 "x1"
[]
 "x1"
[]
 "x1" "x2"
[]
 "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 −

[]
character(0)
[]
 "y3"
[]
 "y1"
[]
character(0)
[]
 "y2" "y4"
[]
 "y2"
[]
 "y2"
[]
 "y1" "y2"
[]
 "y1" "y3" "y4"
[]
 "y3"
[]
character(0)
[]
 "y1"
[]
 "y2"
[]
 "y1" "y4"
[]
 "y2" "y3"
[]
character(0)
[]
 "y2" "y4"
[]
 "y1" "y4"
[]
character(0)
[]
 "y4"