简体   繁体   English

如何根据条件在R中将向量拆分为不相等的块?

[英]How to split a vector into unequal chunks in R based on a condition?

Let's say I have these two vectors: 假设我有以下两个向量:

x <- c(1,2,4,6,7)
y <- c(3,7)

How can I split x into the elements that are smaller than each element of y ? 如何将x拆分为小于y每个元素? For example: c(1,2) | c(4,6,7) 例如: c(1,2) | c(4,6,7) c(1,2) | c(4,6,7) . c(1,2) | c(4,6,7) .

I guess one option would be to do a double loop and return the smallest element in y that is smaller than the current one in x : c(3,3,3,7,7) . 我猜一个选择是做一个双循环并返回y中小于x当前元素的最小元素: c(3,3,3,7,7) I could then split using this vector. 然后,我可以使用此向量拆分。

j <- 1
sapply(x, function(i){
  if (i <= y[j]) {
    y[j]
  } else {
    if (j < length(y)){
      j <- j + 1
    }
    y[j]
  }
})

I feel like there's a cleverer way to do this, but I can't figure it out. 我觉得有一种更聪明的方法可以做到这一点,但我不知道。

Here is how I would do it: 这是我的方法:

x <- c(1,2,4,6,7)
y <- c(3,7)
out <- list(x[x < min(y)], x[!x < min(y)])

Here is the result: 结果如下:

> out
[[1]]
[1] 1 2

[[2]]
[1] 4 6 7

Here is a base R method using split and findInterval : 这是使用splitfindInterval的基本R方法:

split(x, findInterval(x, y, rightmost.closed=TRUE))

$`0`
[1] 1 2

$`1`
[1] 4 6 7

The findInterval function returns a vector that categorizes the variable values in x along your criteria in y. findInterval函数返回一个向量,该向量将x中的变量值与y中的条件进行分类。 The split function separates the vector as desired and returns a named list. split函数根据需要分隔向量,并返回一个命名列表。

Using cut and split in base R: 在基数R中使用cutsplit

lapply(y, function(a) split(x, cut(x, c(-Inf, a, Inf))))

# [[1]]
# [[1]]$`(-Inf,3]`
# [1] 1 2

# [[1]]$`(3, Inf]`
# [1] 4 6 7


# [[2]]
# [[2]]$`(-Inf,7]`
# [1] 1 2 4 6 7

# [[2]]$`(7, Inf]`
# numeric(0)

maybe not the best solution but it is quicker: 也许不是最好的解决方案,但速度更快:

z <- x < min(y)

end <- x[z]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM