R中的grepl函数在R数据帧的字符向量或列的每个元素内搜索与参数模式的匹配项。如果我们想使用grepl对R数据帧的行进行子集化,则可以通过访问包含字符值的列来使用带有单方括号和grepl的子集。
请看以下数据帧:
> 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
> 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