- 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 rows of an R data frame using grepl function?
The grepl function in R search for matches to argument pattern within each element of a character vector or column of an R data frame. If we want to subset rows of an R data frame using grepl then subsetting with single-square brackets and grepl can be used by accessing the column that contains character values.
Example1
Consider the below data frame:
> x1<-sample(c("A","B","C"),20,replace=TRUE) > y1<-rnorm(20,1,0.24) > z1<-rpois(20,2) > df1<-data.frame(x1,y1,z1) > df1
Output
x1 y1 z1 1 A 0.8833979 5 2 B 0.5400075 1 3 C 0.6923827 3 4 B 1.5069186 2 5 B 0.8190962 2 6 B 0.8296171 1 7 B 1.2793876 4 8 B 1.1401782 2 9 C 1.5187263 0 10 C 0.6187501 2 11 B 1.3837516 0 12 C 0.8790544 0 13 A 0.7818624 3 14 B 0.8659361 2 15 B 0.9503166 2 16 A 0.8711020 2 17 B 1.0646814 2 18 A 1.2973144 1 19 C 0.9172171 2 20 B 0.7062629 3
Subsetting df1 by excluding A in x1:
Example
> df1[!grepl("A",df1$x1),]
Output
x1 y1 z1 2 B 0.5400075 1 3 C 0.6923827 3 4 B 1.5069186 2 5 B 0.8190962 2 6 B 0.8296171 1 7 B 1.2793876 4 8 B 1.1401782 2 9 C 1.5187263 0 10 C 0.6187501 2 11 B 1.3837516 0 12 C 0.8790544 0 14 B 0.8659361 2 15 B 0.9503166 2 17 B 1.0646814 2 19 C 0.9172171 2 20 B 0.7062629 3
Example2
> x2<-sample(c("India","China","France"),20,replace=TRUE) > y2<-rexp(20,0.335) > df2<-data.frame(x2,y2) > df2
Output
x2 y2 1 India 2.91693551 2 India 5.86599500 3 China 3.41872121 4 India 6.82404548 5 France 4.26003369 6 China 6.31902445 7 China 2.67848516 8 France 3.20830803 9 India 0.01151151 10 India 2.04166415 11 China 1.72607765 12 China 2.31852068 13 India 1.59578792 14 France 1.06253867 15 China 1.44092496 16 China 2.89259111 17 China 0.16299576 18 France 3.37298728 19 India 0.94687404 20 France 1.26557174
Subsetting df2 by excluding France in x2:
Example
> df2[!grepl("France",df2$x2),]
Output
x2 y2 1 India 2.91693551 2 India 5.86599500 3 China 3.41872121 4 India 6.82404548 6 China 6.31902445 7 China 2.67848516 9 India 0.01151151 10 India 2.04166415 11 China 1.72607765 12 China 2.31852068 13 India 1.59578792 15 China 1.44092496 16 China 2.89259111 17 China 0.16299576 19 India 0.94687404
- Related Articles
- How to subset rows of data frame without NA using dplyr in R?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to subset factor columns in an R data frame?
- How to subset nth row from an R data frame?
- How to subset R data frame rows and keep the rows with NA in the output?
- How to subset rows based on criterion of multiple numerical columns in R data frame?
- How to remove rows in an R data frame using row names?
- How to randomly sample rows from an R data frame using sample_n?
- How to delete rows in an R data frame?
- How to add rows in an R data frame?
- How to subset an R data frame based on small letters?
- How to subset rows of an R data frame if any columns have values greater than a certain value?
- How to subset rows of an R data frame if all columns have values greater than a certain value
- How to convert columns of an R data frame into rows?
- How to remove multiple rows from an R data frame using dplyr package?

Advertisements