如何在多个数组中找到连续的数字?

7

我马上举个例子,假设我有三个数组a、b、c,例如:

a = c(3,5)
b = c(6,1,8,7)
c = c(4,2,9)

我必须能够提取它们之间的连续三元组,即:

c(1,2,3),c(4,5,6)

但这只是一个例子,我将有一个更大的数据集,甚至超过10个数组,因此必须能够找到长度为十的连续系列。

所以,有人能提供一种算法,通常可以在'n'个数组中找到长度为'n'的连续系列。

实际上,我正在使用R进行这些操作,如果您能用R语言编写代码,那就更好了。但任何语言的算法都可以。


3
一个三元组中的每个元素都必须来自不同的数组吗?{2,3,4}会被视为一个有效的三元组吗? - Psidom
是的!{2,3,4},{6,7,8}或{7,8,9}不是有效的。 - Dwarakanath Thoppe
4个回答

7

首先将数据重新组织成包含值和数组编号的列表。对列表进行排序,您将得到类似于:

1-2
2-3
3-1 (i.e. " there' s a three in array 1" )
4-3
5-1
6-2
7-2
8-2
9-3

然后循环列表,检查是否有实际的n个连续数字,然后检查这些数字是否有不同的数组编号。


很棒的想法(已点赞),如果相同的数字在多个向量中出现,可能会有一个困难,但是很容易调整解决方案来解决这个问题 :) - digEmAll

5

这里有一种方法。假设在组数的观测序列中没有中断。以下是数据。

N <- 3
a <- c(3,5)
b <- c(6,1,8,7)
c <- c(4,2,9)

然后我将它们组合在一起,并按观察顺序排序。
dd <- lattice::make.groups(a,b,c)
dd <- dd[order(dd$data),]

现在我要查找表格中所有三个组都有代表的行。
idx <- apply(embed(as.numeric(dd$which),N), 1, function(x) {
    length(unique(x))==N
})

接下来,我们可以看到具有以下特征的三元组:

lapply(which(idx), function(i) {
    dd[i:(i+N-1),]
})

# [[1]]
#    data which
# b2    1     b
# c2    2     c
# a1    3     a
# 
# [[2]]
#    data which
# c1    4     c
# a2    5     a
# b1    6     b

那个例子很完美地运行了。但是你能帮我把这些数组组成一个组吗?因为我有 N 个数组,它们都是 List 中的一个列表。 - Dwarakanath Thoppe

2

这里有一个使用 expand.grid 和三个向量的暴力方法,就像示例中一样。

# get all combinations
df <- expand.grid(a,b,c)

使用 combn 函数计算每对组合的差异。
# get all parwise differences
myDiffs <- combn(names(df), 2, FUN=function(x) abs(x[1]-x[2]))

# subset data using `rowSums` and `which`
df[which(rowSums(myDiffs == 1) == ncol(myDiffs)-1), ]

df[which(rowSums(myDiffs == 1) == ncol(myDiffs)-1), ]
   Var1 Var2 Var3
2     5    6    4
11    3    1    2

有没有想法在 expand.grid() 方法中传递 'N' 列表? - Dwarakanath Thoppe
我刚试了一下,expand.grid可以接受一个向量列表。你可以使用mgetls将向量收集到一个列表中。玩弄一下我的答案这篇文章来构建这样的列表。 - lmo

1
我编写了一个递归函数,可以找出你传入的向量中所有连续的三元组(至少需要传入三个向量)。这个函数可能有点粗糙,但似乎能够正常工作。
该函数使用省略号...来传递参数。因此,它将获取您提供的任意数量的参数(即数字向量),并将它们放在列表items中。然后,找到每个传递向量中的最小值及其索引。
接下来,创建对应于最小三元组的向量的索引,并使用for()循环迭代这些索引,将输出值传递给输出向量out。然后,以递归方式修剪和重新传递items中的输入向量。只有当所有向量都是NA时,也就是向量中没有更多值时,函数才返回最终结果。
library(magrittr)

# define function to find the triplets
tripl <- function(...){
  items <- list(...)

  # find the smallest number in each passed vector, along with its index
  # output is a matrix of n-by-2, where n is the number of passed arguments
  triplet.id <- lapply(items, function(x){
    if(is.na(x) %>% prod) id <- c(NA, NA)
    else id <- c(which(x == min(x)), x[which(x == min(x))])
  }) %>% unlist %>% matrix(., ncol=2, byrow=T)


  # find the smallest triplet from the passed vectors
  index <- order(triplet.id[,2])[1:3]
  # create empty vector for output
  out <- vector()

  # go through the smallest triplet's indices
  for(i in index){
    # .. append the coresponding item from the input vector to the out vector
    # .. and remove the value from the input vector
    if(length(items[[i]]) == 1) {
      out <- append(out, items[[i]])
      # .. if the input vector has no value left fill with NA
      items[[i]] <- NA
    }
    else {
      out <- append(out, items[[i]][triplet.id[i,1]])
      items[[i]] <- items[[i]][-triplet.id[i,1]]
    }
  }

  # recurse until all vectors are empty (NA)
  if(!prod(unlist(is.na(items)))) out <- append(list(out), 
                                                do.call("tripl", c(items), quote = F))
  else(out <- list(out))

  # return result
  return(out)
}

该函数可以通过将输入向量作为参数传递来调用。
# input vectors
a = c(3,5)
b = c(6,1,8,7)
c = c(4,2,9)

# find all the triplets using our function
y <- tripl(a,b,c) 

结果是一个列表,其中包含所有必要的信息,尽管无序。
print(y)
# [[1]]
# [1] 1 2 3
#
# [[2]]
# [1] 4 5 6
# 
# [[3]]
# [1]  7  9 NA
#
# [[4]]
# [1]  8 NA NA

使用sapply()可以完成所有的排序:

# put everything in order
sapply(y, function(x){x[order(x)]}) %>% t
#       [,1] [,2] [,3]
# [1,]    1    2    3
# [2,]    4    5    6
# [3,]    7    9   NA
# [4,]    8   NA   NA

事实上,它将仅使用每个向量的一个值来查找三元组。因此,在例如 c(6,7,11), c(8,9,13)和 c(10,12,14)之间,它将无法找到连续的三元组 c(6,7,8)。在这种情况下,它将返回 c(6,8,10)(见下文)。
a<-c(6,7,11)
b<-c(8,9,13)
c<-c(10,12,14)

y <- tripl(a,b,c)
sapply(y, function(x){x[order(x)]}) %>% t
#     [,1] [,2] [,3]
# [1,]    6    8   10
# [2,]    7    9   12
# [3,]   11   13   14

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接