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)

我努力了:

  • 匹配()
  • all.equal()
  • 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)

对于上面的例子,我需要产生以下输出:

1,6,3,4,5

1,2,3,4,5

1,2,3,4,6

感谢您对此的任何想法。

谢谢

4

2 回答 2

2

我认为您是在说“在另一个整数序列中匹配一个整数序列,其中至少 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
于 2015-12-07T14:01:10.047 回答
0

如果您想在向量中找到与给定向量匹配的唯一元素,您可以使用它%Iin%来测试较大向量中是否存在“模式”。运算符 ,%in%返回一个逻辑向量。将该输出传递给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

请注意,第一个结果与num.pattern.1巧合的顺序相匹配。其他两个向量与模式向量的顺序不匹配。

要找到与模式匹配的确切序列num.data,您可以使用类似于以下函数的内容:

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))
}

为了重现性,我使用set.seed()然后创建了一个测试数据集和模式。该函数find_vector()有两个参数:第一个test.data是您希望检查模式向量的较大数值向量,第二个test.pattern.1是您希望在test.data. 首先,创建了三个列表:lst保存test.data分为长度等于模式向量长度的较小向量,保存满足第一个条件lst2的模式向量,以及保存满足第二个条件的向量。第一个条件测试向量中的元素是否在模式向量中。第二个条件测试来自的向量lstlst3lstlstlst按顺序和元素匹配模式向量。

这种方法的一个问题是NULL当条件不满足时将值引入每个列表,但当条件满足时过程停止。作为参考,您可以打印列表以查看所有测试的向量、满足第一个条件的向量以及满足第二个条件的向量。可以删除空值。删除空值后,找到 和 的交集lst2lst3显示在 中相同匹配的模式test.data

要使用该函数,请确保显式定义test.data <- 'a numeric vector'test.pattern.1 <- 'a numeric vector'。不需要特殊的包装。我没有进行任何基准测试,但该功能似乎运行得很快。我也没有寻找函数失败的场景。

于 2015-12-10T21:56:28.517 回答