- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 an R data frame based on string match in two columns with OR condition?
To subset an R data frame based on string match in two columns with OR condition, we can use grepl function with double square brackets and OR operator |. For example, if we have a data frame called df that contains two string columns say x and y then subsetting based on a particular string match in any of the columns can be done by using the below
Syntax
df[grepl("text",df[["x"]])|grepl("text",df[["y"]]),]
Check out the below examples to understand how it works.
Example1
Consider the below data frame −
f1<-sample(c("India","China","Egypt","UK"),20,replace=TRUE) f2<-sample(c("India","China","Egypt","UK"),20,replace=TRUE) v1<-rnorm(20) df1<-data.frame(f1,f2,v1) df1
Output
f1 f2 v1 1 India India 0.58383357 2 UK Egypt -0.71045054 3 India China -0.07848666 4 Egypt India 1.21017481 5 Egypt UK -0.81991817 6 Egypt China 1.98979283 7 India India 0.36160374 8 Egypt China -1.77619986 9 China UK -0.05397712 10 India Egypt -0.30372078 11 Egypt India -1.68623489 12 India India -0.41997104 13 India China -0.97064798 14 UK Egypt 2.02704796 15 UK Egypt -0.47732133 16 China China 0.53153059 17 Egypt UK -1.71608164 18 Egypt India -0.73298689 19 UK UK 1.83674440 20 China China -1.12186527
Subsetting df1 based on matching of India in any of the first two columns −
df1<-df1[grepl("India",df1[["f1"]])|grepl("India",df1[["f2"]]),] df1
f1 f2 v1 1 India India 0.58383357 3 India China -0.07848666 4 Egypt India 1.21017481 7 India India 0.36160374 10 India Egypt -0.30372078 11 Egypt India -1.68623489 12 India India -0.41997104 13 India China -0.97064798 18 Egypt India -0.73298689
Example2
g1<-sample(c("Male","Female"),20,replace=TRUE) g2<-sample(c("Male","Female"),20,replace=TRUE) v2<-rpois(20,5) df2<-data.frame(g1,g2) df2
Output
g1 g2 1 Female Male 2 Female Male 3 Female Female 4 Male Male 5 Male Female 6 Female Female 7 Female Male 8 Male Male 9 Male Female 10 Male Female 11 Female Female 12 Male Male 13 Male Male 14 Male Female 15 Female Male 16 Female Male 17 Female Male 18 Male Female 19 Female Female 20 Male Female
Subsetting df2 based on matching of Female in any of the first two columns −
df2<-df2[grepl("Female",df2[["g2"]])|grepl("Female",df2[["g2"]]),] df2
g1 g2 3 Female Female 5 Male Female 6 Female Female 9 Male Female 10 Male Female 11 Female Female 14 Male Female 18 Male Female 19 Female Female 20 Male Female
- Related Articles
- How to subset an R data frame based on string values of a columns with OR condition?
- How to subset an R data frame with condition based on only one value from categorical column?
- How to create a subset of an R data frame based on multiple columns?
- How to subset row values based on columns name in R data frame?
- How to delete rows of an R data frame based on string match?
- How to subset an R data frame based on small letters?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to subset factor columns in an R data frame?
- How to subset an R data frame based on numerical and categorical column?
- How to compare two columns in an R data frame for an exact match?
- How to extract columns based on particular column values of an R data frame that match\na pattern?
- How to find the column number of a string column based on a string match in an R data frame?
- How to subset a data frame based on a vector values in R?
- How to find the number of columns of an R data frame that satisfies a condition based on row values?
- How to select data frame columns based on their class in R?

Advertisements