假设我有这两个向量:
x <- c(1,2,4,6,7)
y <- c(3,7)
如何将x
拆分为小于y
每个元素的元素?例如:c(1,2) | c(4,6,7)
。
我想一个选项是做一个双循环并返回y
中小于x
中c(3,3,3,7,7)
当前值的最小元素。j <- 1
sapply(x, function(i){
if (i <= y[j]) {
y[j]
} else {
if (j < length(y)){
j <- j + 1
}
y[j]
}
})
。然后我可以使用这个向量进行分割。
printf("%s %s %s %s", $row[0]);
我觉得有一种更聪明的方法可以做到这一点,但我无法弄明白。
答案 0 :(得分:4)
我将如何做到这一点:
for i in [0..3]
layer = new Layer width:listWidth, height:listHeight, y:i*yDistance, clip:false,
borderRadius: 4, superLayer:canvas
layer.listIndex = i
layer.draggable.enabled = true
layer.draggable.speedX = 0
layer.draggable.speedY = 1
Layers.push(layer)
layer.on Events.DragMove, (event, layer) ->
print layer
print this
结果如下:
x <- c(1,2,4,6,7)
y <- c(3,7)
out <- list(x[x < min(y)], x[!x < min(y)])
答案 1 :(得分:3)
以下是使用split
和findInterval
的基本R方法:
split(x, findInterval(x, y, rightmost.closed=TRUE))
$`0`
[1] 1 2
$`1`
[1] 4 6 7
findInterval
函数返回一个向量,该向量按照y中的条件对x中的变量值进行分类。 split
函数根据需要分离向量并返回命名列表。
答案 2 :(得分:2)
在基地R中使用cut
和split
:
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)
答案 3 :(得分:0)
可能不是最好的解决方案,但速度更快:
z <- x < min(y)
end <- x[z]