如何使用 grepl 函式對 R 資料框中的行進行子集選擇?


R 中的 grepl 函式在字元向量或 R 資料框列的每個元素中搜索與引數模式匹配的內容。如果我們想使用 grepl 對 R 資料框中的行進行子集選擇,可以使用訪問包含字元值的列來使用帶方括號和 grepl 進行子集選擇。

示例 1

考慮以下資料框

即時演示

> 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

輸出

  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

透過在 x1 中排除 A 對 df1 進行子集選擇

示例

> df1[!grepl("A",df1$x1),]

輸出

  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

示例 2

即時演示

> x2<-sample(c("India","China","France"),20,replace=TRUE)
> y2<-rexp(20,0.335)
> df2<-data.frame(x2,y2)
> df2

輸出

   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

透過在 x2 中排除法國對 df2 進行子集選擇

示例

> df2[!grepl("France",df2$x2),]

輸出

    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

更新日期:2020 年 11 月 23 日

4K+ 瀏覽量

開啟你的 事業

完成課程以獲得認證

開始學習
廣告
© . All rights reserved.