- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 and row names in the R data frame based on condition.
To find the column names and row names in an R data frame based on a condition, we can use row.names and colnames function. The condition for which we want to find the row names and column names can be defined inside these functions as shown in the below Examples.
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,1) x2<-rpois(20,8) x3<-rpois(20,20) df1<-data.frame(x1,x2,x3) df1
The following dataframe is created
x1 x2 x3 1 1 11 15 2 3 6 15 3 0 9 18 4 2 10 26 5 1 9 17 6 1 7 23 7 0 11 21 8 2 11 23 9 1 6 22 10 2 6 28 11 2 7 22 12 0 8 16 13 1 7 28 14 1 7 25 15 0 6 14 16 0 10 23 17 0 12 16 18 0 8 23 19 2 8 17 20 2 8 21
To find which rows has value 17 in it on the above created data frame, add the following code to the above snippet −
x1<-rpois(20,1) x2<-rpois(20,8) x3<-rpois(20,20) df1<-data.frame(x1,x2,x3) row.names(df1[which(df1==17,arr.ind=T)[,1],] )
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "5" "19"
To find which columns has value 17 in it on the above created data frame, add the following code to the above snippet −
x1<-rpois(20,1) x2<-rpois(20,8) x3<-rpois(20,20) df1<-data.frame(x1,x2,x3) colnames(df1)[apply(df1, 2, function(x) any(x==17))]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "x3"
Example 2
Following snippet creates a sample data frame −
y1<-sample(1:100,20) y2<-sample(1:100,20) y3<-sample(1:100,20) df2<-data.frame(y1,y2,y3) df2
The following dataframe is created
y1 y2 y3 1 75 52 32 2 37 5 46 3 43 31 60 4 100 30 11 5 28 18 79 6 31 80 53 7 8 85 49 8 62 38 56 9 48 82 15 10 97 48 69 11 69 25 40 12 12 92 21 13 77 55 26 14 39 95 63 15 82 61 75 16 98 40 14 17 61 78 22 18 93 63 58 19 10 21 17 20 2 68 83
To find which rows has value 92 in it on the above created data frame, add the following code to the above snippet −
y1<-sample(1:100,20) y2<-sample(1:100,20) y3<-sample(1:100,20) df2<-data.frame(y1,y2,y3) row.names(df2[which(df2==92,arr.ind=T)[,1],] )
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "12"
To find which columns have value 92 in it on the above created data frame, add the following code to the above snippet −
y1<-sample(1:100,20) y2<-sample(1:100,20) y3<-sample(1:100,20) df2<-data.frame(y1,y2,y3) colnames(df2)[apply(df2, 2, function(x) any(x==92))]
Output
If you execute all the above given snippets as a single program, it generates the following Output −
[1] "y2"
- Related Articles
- How to change the column names and row names of a data frame in R?
- How to select the first and last row based on group column in an R data frame?
- Convert a numeric column to binary factor based on a condition in R data frame
- How to change row values based on column values in an R data frame?
- How to find the number of columns of an R data frame that satisfies a condition based on row values?
- Replace each value in a column with the largest value based on a condition in R data frame.
- How to create a new column in an R data frame based on some condition of another column?
- How to sort an R data frame column without losing row names?
- How to get row index based on a value of an R data frame column?
- How to subset an R data frame with condition based on only one value from categorical column?
- How to find the minimum for each row based on few columns in an R data frame?
- How to divide row values of a numerical column based on categorical column values in an R data frame?
- How to convert a matrix into a data frame with column names and row names as variables in R?
- Replace numerical column values based on character column values in R data frame.
- How to get row index or column index based on their names in R?
