- 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
How to subset row values based on columns name in R data frame?
To subset row values based on columns name in R data frame, we can follow the below steps −
- First of all, create a data frame.
- Then, create a sample of column names of size equal to number of rows in the data frame and row names in the same manner with seq_len function, after that use cbind function to subset the rows based on columns name.
Create the data frame
Let's create a data frame as shown below −
Gender_1<-sample(c("Male","Female"),20,replace=TRUE) Gender_2<-sample(c("Male","Female","Unknown"),20,replace=TRUE) df<-data.frame(Gender_1,Gender_2) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Gender_1 Gender_2 1 Female Female 2 Female Female 3 Male Male 4 Male Male 5 Male Male 6 Male Female 7 Female Male 8 Female Female 9 Male Male 10 Female Male 11 Male Female 12 Female Male 13 Male Female 14 Female Female 15 Female Male 16 Female Female 17 Female Female 18 Female Female 19 Male Unknown 20 Female Female
Subset the rows based on columns name
Using sample function to create a sample column names and row names in the same manner with seq_len function, after that using cbind function to subset the rows based on columns name created with sample function −
Gender_1<-sample(c("Male","Female"),20,replace=TRUE) Gender_2<-sample(c("Male","Female","Unknown"),20,replace=TRUE) df<-data.frame(Gender_1,Gender_2) Columns<-sample(c("Gender_1","Gender_2"),20,replace=TRUE) rownames(df)<-seq_len(nrow(df)) df[cbind(rownames(df),Columns)]
Output
[1] "Female" "Female" "Male" "Male" "Male" "Female" "Female" "Female" [9] "Male" "Female" "Female" "Female" "Male" "Female" "Female" "Female" [17] "Female" "Female" "Male" "Female"
- Related Articles
- How to subset matrix row values based on different columns?
- How to subset a data frame based on a vector values in R?
- How to subset an R data frame based on string values of a columns with OR condition?
- How to create a subset of an R data frame based on multiple columns?
- How to change row values based on column values in an R data frame?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to subset an R data frame based on small letters?
- How to subset an R data frame based on string match in two columns with OR condition?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to find the number of columns of an R data frame that satisfies a condition based on row values?
- How to find the minimum for each row based on few columns in an R data frame?
- How to subset factor columns in an R data frame?
- How to select data frame columns based on their class in R?
- How to subset an R data frame based on numerical and categorical column?
- How to subset nth row from an R data frame?

Advertisements