通过使用单个方括号进行子集设置,可以轻松提取R数据帧的列值,但如果要提取与模式匹配的列值,则需要在单个方括号内使用grepl函数,这将有助于我们匹配数据框列中值的模式。
请看以下数据帧:
> set.seed(271) > x1<-sample(c("A187","A321","A123","A242"),20,replace=TRUE) > x2<-sample(c("B87","B74","B81","B71"),20,replace=TRUE) > df1<-data.frame(x1,x2) > df1
输出结果
x1 x2 1 A242 B71 2 A123 B71 3 A242 B81 4 A242 B87 5 A123 B71 6 A321 B71 7 A187 B74 8 A321 B74 9 A123 B71 10 A242 B74 11 A321 B74 12 A187 B87 13 A321 B74 14 A321 B74 15 A123 B74 16 A187 B87 17 A123 B81 18 A242 B87 19 A242 B71 20 A187 B74
提取以A1开头的值:
> df1[grepl("A1",x1),]
输出结果
x1 x2 2 A123 B71 5 A123 B71 7 A187 B74 9 A123 B71 12 A187 B87 15 A123 B74 16 A187 B87 17 A123 B81 20 A187 B74
提取以A2开头的值:
> df1[grepl("A2",x1),]
输出结果
x1 x2 1 A242 B71 3 A242 B81 4 A242 B87 10 A242 B74 18 A242 B87 19 A242 B71
提取以A3开头的值:
> df1[grepl("A3",x1),]
输出结果
x1 x2 6 A321 B71 8 A321 B74 11 A321 B74 13 A321 B74 14 A321 B74
提取以B7开头的值:
> df1[grepl("B7",x2),]
输出结果
x1 x2 1 A242 B71 2 A123 B71 5 A123 B71 6 A321 B71 7 A187 B74 8 A321 B74 9 A123 B71 10 A242 B74 11 A321 B74 13 A321 B74 14 A321 B74 15 A123 B74 19 A242 B71 20 A187 B74
提取以B8开头的值:
> df1[grepl("B8",x2),]
输出结果
x1 x2 3 A242 B81 4 A242 B87 12 A187 B87 16 A187 B87 17 A123 B81 18 A242 B87
让我们看另一个例子:
> y1<-sample(c("ID1","ID2","ID3","ID4"),20,replace=TRUE) > y2<-sample(c("C1","C2"),20,replace=TRUE) > df2<-data.frame(y1,y2) > df2
输出结果
y1 y2 1 ID1 C1 2 ID3 C1 3 ID1 C1 4 ID2 C2 5 ID4 C2 6 ID2 C2 7 ID2 C1 8 ID3 C1 9 ID1 C1 10 ID1 C1 11 ID2 C2 12 ID2 C2 13 ID4 C1 14 ID3 C2 15 ID2 C1 16 ID4 C1 17 ID4 C1 18 ID2 C2 19 ID2 C2 20 ID3 C2
> df2[grepl("ID1",y1),]
输出结果
y1 y2 1 ID1 C1 3 ID1 C1 9 ID1 C1 10 ID1 C1
> df2[grepl("ID2",y1),]
输出结果
y1 y2 4 ID2 C2 6 ID2 C2 7 ID2 C1 11 ID2 C2 12 ID2 C2 15 ID2 C1 18 ID2 C2 19 ID2 C2
> df2[grepl("ID3",y1),]
输出结果
y1 y2 2 ID3 C1 8 ID3 C1 14 ID3 C2 20 ID3 C2
> df2[grepl("ID4",y1),]
输出结果
y1 y2 5 ID4 C2 13 ID4 C1 16 ID4 C1 17 ID4 C1
> df2[grepl("C1",y2),]
输出结果
y1 y2 1 ID1 C1 2 ID3 C1 3 ID1 C1 7 ID2 C1 8 ID3 C1 9 ID1 C1 10 ID1 C1 13 ID4 C1 15 ID2 C1 16 ID4 C1 17 ID4 C1
> df2[grepl("C2",y2),]
输出结果
y1 y2 4 ID2 C2 5 ID4 C2 6 ID2 C2 11 ID2 C2 12 ID2 C2 14 ID3 C2 18 ID2 C2 19 ID2 C2 20 ID3 C2