整数数据序列中的近似模式匹配与R
我有一个整数模式整数数据序列中的近似模式匹配与R,r,sequence,matching,extraction,approximate,R,Sequence,Matching,Extraction,Approximate,我有一个整数模式c(1,2,3,4,5),需要在数据中近似匹配c(1,10,1,6,3,4,5,1,2,3,4,5,9,10,1,2,3,4,6) 我试过: pmatch() 全部相等 grepl() 但他们似乎不支持这种情况 pattern <- c(1,2,3,4,5) data <- c(1,10,1,6,3,4,5,1,2,3,4,5,9,10,1,2,3,4,6) pattern我想你说的是“在另一个整数序列中匹配一个整数序列,其中至少有N-1个整数匹配”。在重叠匹
c(1,2,3,4,5)
,需要在数据中近似匹配c(1,10,1,6,3,4,5,1,2,3,4,5,9,10,1,2,3,4,6)
我试过:
- pmatch()
- 全部相等
- grepl()
pattern <- c(1,2,3,4,5)
data <- c(1,10,1,6,3,4,5,1,2,3,4,5,9,10,1,2,3,4,6)
pattern我想你说的是“在另一个整数序列中匹配一个整数序列,其中至少有N-1个整数匹配”。在重叠匹配的情况下,行为应该是什么还不清楚,因此下面将选取重叠的序列
# helper function to test "match" at a threshold of 4 matches
is_almost <- function(s1, s2, thresh = 4) {
sum(s1 == s2) >= thresh }
# function to lookup and return sequences
extract_seq <- function(pattern, data) {
res <- lapply(1:(length(data) - length(pattern) + 1), function(s) {
subseq <- data[s:(s+length(pattern)-1)]
if (is_almost(pattern, subseq)) {
subseq}
})
Filter(Negate(is.null),res)
}
# let's test it out
pattern <- c(1,2,3,4,5)
data <- c(1,10,1,6,3,4,5,1,2,3,4,5,9,10,1,2,3,4,6)
extract_seq(pattern,data)
[[1]]
[1] 1 6 3 4 5
[[2]]
[1] 1 2 3 4 5
[[3]]
[1] 1 2 3 4 6
#在4个匹配阈值下测试“匹配”的辅助函数
是_几乎=thresh}
#用于查找和返回序列的函数
提取_seq如果您想在向量中找到与给定向量匹配的唯一元素,您可以使用%Iin%
测试在较大向量中是否存在您的“模式”。运算符%
中的%返回一个逻辑向量。将该输出传递给which()
将返回每个TRUE
值的索引,该值可用于将较大的向量子集,以返回与“模式”匹配的所有元素,而不考虑顺序。将子集向量传递给unique()
可消除重复项,以便与“模式”向量的元素和长度匹配的较大向量中的每个元素只出现一次
例如:
> num.data <- c(1, 10, 1, 6, 3, 4, 5, 1, 2, 3, 4, 5, 9, 10, 1, 2, 3, 4, 5, 6)
> num.pattern.1 <- c(1,6,3,4,5)
> num.pattern.2 <- c(1,2,3,4,5)
> num.pattern.3 <- c(1,2,3,4,6)
> unique(num.data[which(num.data %in% num.pattern.1)])
[1] 1 6 3 4 5
> unique(num.data[which(num.data %in% num.pattern.2)])
[1] 1 3 4 5 2
> unique(num.data[which(num.data %in% num.pattern.3)])
[1] 1 6 3 4 2
为了再现性,我使用了set.seed()
,然后创建了一个测试数据集和模式。函数find_vector()
有两个参数:第一个参数是test.data
,它是要检查模式向量的较大数值向量;第二个参数是test.pattern.1
它是要在test.data
中查找的较短数值向量。首先,创建三个列表:lst
以保存测试。将数据
划分为长度等于模式向量长度的较小向量,lst2
以保存满足第一个条件的lst
模式向量,和lst3
以从lst
保存满足第二个条件的向量。第一个条件测试lst
中向量的元素是否在模式向量中。第二个条件测试lst
中的向量是否按顺序和元素匹配模式向量
这种方法的一个问题是,当条件不满足时,NULL
值被引入每个列表,但当条件满足时,过程停止。为了便于参考,您可以打印列表以查看所有测试向量、满足第一个条件的向量以及满足第二个条件的向量。可以删除空值。清除空值后,找到lst2
和lst3
的交点将显示test.data
中相同匹配的模式
要使用该函数,请确保明确定义test.data如何获得这些输出尚不清楚。请解释您是如何从输入转换到输出的。@RichardScriven-非常不清楚,但它似乎是成套匹配的,即-删除第一批最接近的匹配项,然后重新开始1:5
与1,6,3,4,5
匹配得非常接近,然后1,2,3,4,5
,然后1,2,3,4,6
类似于此的近似版本:如何处理重叠序列,例如:c(1,2,3,4,1,2,3,4,5)
,谢谢Gary,你提供的正是我想要的。我用50万的数字向量(数据)尝试了Gary的解决方案。只花了6秒钟就得出了结果。
set.seed(12102015)
test.data <- sample(c(1:99), size = 500, replace = TRUE)
test.pattern.1 <- test.data[90:94]
find_vector <- function(test.data, test.pattern.1) {
# List of all the vectors from test.data with length = length(test.pattern.1), currently empty
lst <- vector(mode = "list")
# List of vectors that meet condition 1, currently empty
lst2 <- vector(mode = "list")
# List of vectors that meet condition 2, currently empty
lst3 <- vector(mode = "list")
# A modifier to the iteration variable used to build 'lst'
a <- length(test.pattern.1) - 1
# The loop to iterate through 'test.data' testing for conditions and building lists to return a match
for(i in 1:length(test.data)) {
# The list is build incrementally as 'i' increases
lst[[i]] <- test.data[c(i:(i+a))]
# Conditon 1
if(sum(lst[[i]] %in% test.pattern.1) == length(test.pattern.1)) {lst2[[i]] <- lst[[i]]}
# Condition 2
if(identical(lst[[i]], test.pattern.1)) {lst3[[i]] <- lst[[i]]}
}
# Remove nulls from 'lst2' and 'lst3'
lst2 <- lst2[!sapply(lst2, is.null)]
lst3 <- lst3[!sapply(lst3, is.null)]
# Return the intersection of 'lst2' and 'lst3' which should be a match to the pattern vector.
return(intersect(lst2, lst3))
}