Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在两个不同的向量中按顺序匹配数字_R_Loops - Fatal编程技术网

R 在两个不同的向量中按顺序匹配数字

R 在两个不同的向量中按顺序匹配数字,r,loops,R,Loops,这个标题并没有真正公正地回答这个问题,但我想不出任何其他方式来表达这个问题。我可以用一个例子来最好地解释这个问题 假设我们有两个数字向量(每个向量总是升序且唯一): 我目前的解决方案是这样的,但我相信它可能效率很低: # establishes where we start from the vector2 numbers # just in case we have vector1 <- c(5,8,10) # and vector2 <- c(1,2,3,4,6,7). We w

这个标题并没有真正公正地回答这个问题,但我想不出任何其他方式来表达这个问题。我可以用一个例子来最好地解释这个问题

假设我们有两个数字向量(每个向量总是升序且唯一):

我目前的解决方案是这样的,但我相信它可能效率很低:

# establishes where we start from the vector2 numbers
# just in case we have vector1 <- c(5,8,10)
# and vector2 <- c(1,2,3,4,6,7). We would want to skip the 1,2,3,4 values

  i <- 1
  while(vector2[i]<vector1[1]){
    i <- i+1
  }

# starts the result1 vector with the first value from the vector1

  result1 <- vector1[1]

# starts the result2 vector empty and will add as we loop through

  result2 <- c()


# super complicated and probably hugely inefficient loop within a loop within a loop 
# i really want to avoid doing this, but I cannot think of any other way to accomplish this

  for(j in 1:length(vector1)){

    while(vector1[j] > vector2[i] && (i+1) <= length(vector2)){

      result1 <- c(result1,vector1[j])
      result2 <- c(result2,vector2[i])         

      while(vector1[j] > vector2[i+1] && (i+2) <= length(vector2)){

        i <- i+1
      }
      i <- i+1
    }
  }

  ## have to add on the last vector2 value cause while loop skips it
  ## if it doesn't exist (there are no more vector2 values bigger) we put in an NA

  if(result1[length(result1)] < vector2[i]){
    result2 <- c(result2,vector2[i])
  }
  else{
    ### we ran out of vector2 values that are bigger 
    result2 <- c(result2,NA)
  }
#确定从矢量2数字开始的位置

#万一我们有vector1这真的很难解释。就叫它魔术吧:)


vector1这是一个非常聪明的解决方案。我从未想过将这两个向量交错(我想我没有充分利用我的上升和唯一性假设)。我刚刚更改了最后一行代码,并使其正常工作。非常感谢你!很抱歉打扰您,但我在尝试将其与以前的解决方案进行比较时注意到一个bug。如果vector1值开始高于vector2值,则输出不正确。例如,vector1=(3,10,…)和vector2是(1,5,9…),我通过简单地调整vector2来移除任何低于vector1初始值的值来修复这个问题,你可以只使用vector2作为向量1,反之亦然?也就是说,始终先使用第一个元素最小的向量?是的,这也应该有效!Vector1在我的代码中应用了这个概念,但它有一个特定的含义,所以我试图以一种特定的方式对它们进行排序。但仅就这个问题而言,这可能是一个更好的解决方案。添加这一行可能适用于这种情况
if(any(idx=vector2)))vector2
result1 = c(1,10,24,30)
result2 = c(5,15,28,35)
# establishes where we start from the vector2 numbers
# just in case we have vector1 <- c(5,8,10)
# and vector2 <- c(1,2,3,4,6,7). We would want to skip the 1,2,3,4 values

  i <- 1
  while(vector2[i]<vector1[1]){
    i <- i+1
  }

# starts the result1 vector with the first value from the vector1

  result1 <- vector1[1]

# starts the result2 vector empty and will add as we loop through

  result2 <- c()


# super complicated and probably hugely inefficient loop within a loop within a loop 
# i really want to avoid doing this, but I cannot think of any other way to accomplish this

  for(j in 1:length(vector1)){

    while(vector1[j] > vector2[i] && (i+1) <= length(vector2)){

      result1 <- c(result1,vector1[j])
      result2 <- c(result2,vector2[i])         

      while(vector1[j] > vector2[i+1] && (i+2) <= length(vector2)){

        i <- i+1
      }
      i <- i+1
    }
  }

  ## have to add on the last vector2 value cause while loop skips it
  ## if it doesn't exist (there are no more vector2 values bigger) we put in an NA

  if(result1[length(result1)] < vector2[i]){
    result2 <- c(result2,vector2[i])
  }
  else{
    ### we ran out of vector2 values that are bigger 
    result2 <- c(result2,NA)
  }
vector1 <- c(1,3,10,11,24,26,30,31)
vector2 <- c(5,9,15,19,21,23,28,35)
## another case
# vector2 <- c(0,9,15,19,21,23,28,35)

## handling the case where vector2 min value(s) are < vector1 min value
if (any(idx <- which(min(vector1) >= vector2))) 
   vector2 <- vector2[-idx]

## interleave the two vectors
tmp <- c(vector1,vector2)[order(c(order(vector1), order(vector2)))]

## if we sort the vectors, which pairwise elements are from the same vector
r <- rle(sort(tmp) %in% vector1)$lengths

## I want to "remove" all the pairwise elements which are from the same vector
## so I again interleave two vectors:
## the first will be all TRUEs because I want the first instance of each *new* vector
## the second will be all FALSEs identifying the elements I want to throw out because
## there is a sequence of elements from the same vector
l <- rep(1, length(r))
ord <- c(l, r - 1)[order(c(order(r), order(l)))]

## create some dummy TRUE/FALSE to identify the ones I want
res <- sort(tmp)[unlist(Map(rep, c(TRUE, FALSE), ord))]

setNames(split(res, res %in% vector2), c('result1', 'result2'))

# $result1
# [1]  1 10 24 30
# 
# $result2
# [1]  5 15 28 35
vector1 <- c(1,3,10,11,24,26,30,31)
vector2 <- c(5,9,15,19,21,23,28,35)
vector2 <- c(0,9,15,19,21,23,28,35)
vector2 <- c(1,3,3,5,7,9,28,35)

f <- function(v1, v2) {
  if (any(idx <- which(min(vector1) >= vector2))) 
    vector2 <- vector2[-idx]

  vector1 <- paste0(vector1, '.0')
  vector2 <- paste0(vector2, '.00')

  n <- function(x) as.numeric(x)

  tmp <- c(vector1, vector2)[order(n(c(vector1, vector2)))]

  m <- tmp[1]
  idx <- c(TRUE, sapply(1:(length(tmp) - 1), function(x) {
    if (n(tmp[x + 1]) > n(m)) {
      if (gsub('^.*\\.','', tmp[x + 1]) == gsub('^.*\\.','', m)) 
        FALSE
      else {
        m <<- tmp[x + 1]
        TRUE
      }
    } else FALSE
  }))

  setNames(split(n(tmp[idx]), grepl('\\.00$', tmp[idx])), c('result1','result2'))
}
f(vector1, vector2)

# $result1
# [1]  1 10 30
# 
# $result2
# [1]  3 28 35