[英]selecting rows in a data.frame in which a certain column has values containing one of a set of prefixes
I have a data.frame of the type: 我有一个类型的data.frame:
> head(engschools)
RECTYPE LEA ESTAB URN SCHNAME TOWN PCODE
1 1 919 2028 138231 Alban City School n.a. E1 3RR
2 1 919 4003 138582 Samuel Ryder Academy St Albans AL1 5AR
3 1 919 2004 138201 Hatfield Community Free School Hatfield AL10 8ES
4 2 919 7012 117671 St Luke's School n.a BR3 7ET
5 1 919 2018 138561 Harpenden Free School Redbourn AL3 7QA
6 2 919 7023 117680 Lakeside School Welwyn Garden City AL8 6YN
And a set of prefixes like this one: 还有一组这样的前缀:
>head(prefixes)
E
AL
I would like to select the rows from the data.frame engschools that have values in column PCODE which contain one of the prefixes in prefixes . 我想选择那些在列PCODE其中包含的前缀前缀的一个值data.frame engschools行。 The correct result would thus contain rows
1:3
and 5:6
but not row 4
. 正确的结果将因此包含行
1:3
和5:6
,而不包含行4
。
You can try something like this: 您可以尝试如下操作:
mydf[grep(paste0("^", prefixes, collapse="|"), engschools$PCODE), ]
# RECTYPE LEA ESTAB URN SCHNAME TOWN PCODE
# 1 1 919 2028 138231 Alban City School n.a. E1 3RR
# 2 1 919 4003 138582 Samuel Ryder Academy St Albans AL1 5AR
# 3 1 919 2004 138201 Hatfield Community Free School Hatfield AL10 8ES
# 5 1 919 2018 138561 Harpenden Free School Redbourn AL3 7QA
# 6 2 919 7023 117680 Lakeside School Welwyn Garden City AL8 6YN
Here, we have used: 在这里,我们使用了:
paste
to create our search pattern (in this case, "^E|^AL"
). paste
以创建我们的搜索模式(在本例中为"^E|^AL"
)。 grep
to identify the row indexes that match the provided pattern. grep
标识与提供的模式匹配的行索引。 [
style extracting to extract the relevant rows. [
样式提取可提取相关行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.