简体   繁体   English

如果字符串包含特定字符的特定字符 position(在 R 中),则进行过滤和子集

[英]Filter & Subset if a String Contains Certain Characters at specific position (in R)

I currently wish to subset a data frame if it contains any numbers from 01 to 12 at 11-12 position (if we also consider - as a character then the position will be 14-15th position).如果数据框在 11-12 position 包含从 01 到 12 的任何数字,我目前希望对数据框进行子集化(如果我们也考虑 - 作为一个字符,那么 position 将位于第 14-15 位)。 I tried grepl but was not able to do it successfully.我试过 grepl 但没能成功。

Data sample:数据样本:

x <- data.table(c('ACCN-NJ-A55O-01A-11D-A25L-08','ACCN-NJ-A55O-11D-11D-A25L-08', 'ACCN-05-4249-01A-01D-1105-08', 'ACCN-S2-AA1A-15C-12D-A397-08'))

Expected Output (row number 1, 2 and 3 will returned):预计 Output(将返回行号 1、2 和 3):

ACCN-NJ-A55O-01A-11D-A25L-08
ACCN-NJ-A55O-11D-11D-A25L-08
ACCN-05-4249-01A-01D-1105-08

Any help would be appreciated.任何帮助,将不胜感激。 Thanks in advance提前致谢

If the position is fixed you can use substr / substring to extract string at specific position.如果 position 是固定的,您可以使用substr / substring提取特定 position 处的字符串。

subset(x, as.integer(substr(V1, 14, 15)) <= 12)

#                             V1
#1: ACCN-NJ-A55O-01A-11D-A25L-08
#2: ACCN-NJ-A55O-11D-11D-A25L-08
#3: ACCN-05-4249-01A-01D-1105-08

Using dplyr -使用dplyr -

library(dplyr)
x %>% filter(between(as.integer(substr(V1, 14, 15)), 1, 12))

Using square bracket subsetting:使用方括号子集:

x[as.integer(substr(x$V1, 14, 15)) <= 12,]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM