How to subset rows of an R data frame using grepl function?

R ProgrammingServer Side ProgrammingProgramming

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:

Live Demo

> 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

Live Demo

> 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
raja
Published on 23-Nov-2020 10:37:07
Advertisements