- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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"

- Related Questions & Answers
- Find the column name with the largest value for each row in an R data frame.
- Find the column name of least value in each row of an R dataframe.
- How to create a column with column name for maximum value in each row of an R data frame?
- How to delete a row from an R data frame if any value in the row is greater than n?
- How to remove row that contains maximum for each column in R data frame?
- Find the value in an R data frame column that exist n times.
- Create a quartile column for each value in an R data frame column.
- How to create a column of first non-zero value in each row of an R data frame?
- How to find the row and column index of a character value in an R data frame?
- How to find the frequency of each value in an R data frame?
- Find the column index of least value for each row of an R matrix
- How to remove a column from a data frame that contains same value in R?
- How to add a new column to an R data frame with largest value in each row?
- How to name a data frame column with a vector value that has the same name in R?
- Replace all values in an R data frame if they are greater than a certain value.