在data.frame中选择行,其中某一列的值包含一组前缀之一 [英] selecting rows in a data.frame in which a certain column has values containing one of a set of prefixes

查看:71
本文介绍了在data.frame中选择行,其中某一列的值包含一组前缀之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.frame类型:

I have a data.frame of the type:

> 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 

和一组这样的前缀:

>head(prefixes)
E
AL

我想从data.frame中选择行在 PCODE 列中具有值的em> engschools ,其中包含前缀中的前缀之一。正确的结果将因此包含行 1:3 5:6 ,但不包含行 4

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. The correct result would thus contain rows 1:3 and 5:6 but not row 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

这里,我们已经使用:


  • 粘贴来创建搜索模式(在这种情况下, ^ E | ^ AL )。

  • grep

  • 基本 [样式提取以提取相关行。

  • paste to create our search pattern (in this case, "^E|^AL").
  • grep to identify the row indexes that match the provided pattern.
  • Basic [ style extracting to extract the relevant rows.

这篇关于在data.frame中选择行,其中某一列的值包含一组前缀之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆