我有两个值,例如:
from = XY05*
to = XY55*
然后我有一个包含很多字符串的tbl_dff
。
Codes = ["XY05A", "XY56", "XY555", "AT003", "XY55AB", "XY35QA"
"GA003A", "XY36", "XY100", "XY03",...]
我想使用我的变量from
和to
来查看这些是否在我的Codes
变量中。
从示例中,我希望有一个匹配:
"XY05A"
"XY555"
"XY36"
"XY55AB"
"XY35QA"
因为它介于XY05* - XY55*
之间。 *
只是说,我不在乎后者是什么。
希望这是有道理的。
你能用from
和to
作为整数而不是完整的字符串吗?这样你就可以从Codes
向量中提取整数,并将它们直接与from
和to
进行比较:
from <- 5
to <- 55
pattern <- "XY([0-9]+).*"
# use regex to extract the integer part of each string
Codes_int <- as.integer( sub( pattern, "\\1", Codes ) )
# return only the `Codes` where the integer is in range
Codes[ Codes_int >= from & Codes_int <= to & grepl( pattern, Codes ) ]