R 在两个不同的向量之间交替选择,同时确保每个选择都大于上一个选择

R 在两个不同的向量之间交替选择,同时确保每个选择都大于上一个选择,r,R,我需要在两个不同的向量之间交替选择数字。首先,我全局选择最小的数字,然后根据第一个数字来自何处,下一个数字需要来自另一个向量,并大于之前的选择。这意味着在每一次新的选择中,我必须做两件事。1) 数字必须来自另一个向量2)它必须大于最后一个选择 范例 a <- c(4,7,12,24,27,28,37,50,53) b <- c(10,14,22,32,36,41,45,47,48,51,54,59,63,68) 以下是一种使用存储矢量号的附加矢量和diff查找矢量号中的开关的方法:

我需要在两个不同的向量之间交替选择数字。首先,我全局选择最小的数字,然后根据第一个数字来自何处,下一个数字需要来自另一个向量,并大于之前的选择。这意味着在每一次新的选择中,我必须做两件事。1) 数字必须来自另一个向量2)它必须大于最后一个选择

范例

a <- c(4,7,12,24,27,28,37,50,53)
b <- c(10,14,22,32,36,41,45,47,48,51,54,59,63,68)

以下是一种使用存储矢量号的附加矢量和
diff
查找矢量号中的开关的方法:

a <- c(4,7,12,24,27,28,37,50,53)
b <- c(10,14,22,32,36,41,45,47,48,51,54,59,63,68)

target <- c(4,10,12,14,24,32,37,41,50,51,53,54)

selectNumbers <- function(x, y) {
  vec <- c(x, y)
  ## create an vector containing the index/number of vec
  idx <- rep(1:2, c(length(x), length(y)))

  ## sort both combined vectors by vec
  o <- order(vec)
  sortedVec <- vec[o]
  sortedIdx <- idx[o]

  ## if the diff == 0 the index doesn't change => excluded
  sel <- c(TRUE, diff(sortedIdx) != 0)

  return(sortedVec[sel])
}

identical(selectNumbers(a, b), target)
# TRUE

a非常感谢Jilber,我读了好几遍,但我不明白其中的逻辑。你介意解释一下它是如何工作的吗。我看不出它在哪里做了比较来选择下一个更大的数字。甚至从一个向量到另一个向量的交替过程也不清楚。它运行良好,但我不理解其逻辑。@user2004820:我不是Jilber,但请查看我的编辑以获得澄清。我现在收到了,非常感谢。我没有注意的是顺序函数。起初,我不明白在我们的示例中是什么顺序产生了“o”。现在我非常感谢你。
a <- c(4,7,12,24,27,28,37,50,53)
b <- c(10,14,22,32,36,41,45,47,48,51,54,59,63,68)

target <- c(4,10,12,14,24,32,37,41,50,51,53,54)

selectNumbers <- function(x, y) {
  vec <- c(x, y)
  ## create an vector containing the index/number of vec
  idx <- rep(1:2, c(length(x), length(y)))

  ## sort both combined vectors by vec
  o <- order(vec)
  sortedVec <- vec[o]
  sortedIdx <- idx[o]

  ## if the diff == 0 the index doesn't change => excluded
  sel <- c(TRUE, diff(sortedIdx) != 0)

  return(sortedVec[sel])
}

identical(selectNumbers(a, b), target)
# TRUE
## create a combined vector
vec <- c(a, b)
# [1]  4  7 12 24 27 28 37 50 53 10 14 22 32 36 41 45 47 48 51 54 59 63 68

## create a vector which contains the origin of each number
idx <- rep(1:2, c(length(a), length(b)))
# [1] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2

## now we sort the combined vector because our selected numbers should increase
o <- order(vec)
sortedVec <- vec[o]
# [1]  4  7 10 12 14 22 24 27 28 32 36 37 41 45 47 48 50 51 53 54 59 63 68

## but we have to know which number was in which original vector (a/b)
## that's why we sort our index vector, too
sortedIdx <- idx[o]
# [1] 1 1 2 1 2 2 1 1 1 2 2 1 2 2 2 2 1 2 1 2 2 2 2

## now the critical part: we want to select alternatively
## that's why we exclude every consecutive number in sortedIdx
## (means every sortedIdx[i] == sortedIdx[i+1] => diff(sortedIdx[i:(i+1)]) == 0)
diff(sortedIdx)
# [1]  0  1 -1  1  0 -1  0  0  1  0 -1  1  0  0  0 -1  1 -1  1  0  0  0

## because the diff output has a length of n-1 we need to add the first element
## (which should be the global minimum and will be selected always)
sel <- c(TRUE, diff(sortedIdx) != 0)
sel
# [1]  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE
# [16] FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE

## now we return only the selected elements
sortedVec[sel]
# [1]  4 10 12 14 24 32 37 41 50 51 53 54