值小于1时向量中的拉伸次数
The number of stretches in the vector when the value is less than 1
我问过类似的问题。我想在值小于 1 时找到块数。在下面给出的示例中,答案必须是 2。
Block #1: 0.2,0.3
Block #2: 0.25959969,0.23051247,0.33962332
但是,只有当我说 values==0
时,该解决方案才有效。如果我使用小于号,即 values<1
,则答案不正确 ( 0),而它必须是 2.
这里我提供了可重现的例子:
x <- c(0.2,0.3,9.36401452,7.55982857,6.06992455,4.67168492,3.50976922,2.18683333,1.36610395,0.25959969,0.23051247,0.33962332)
with(rle(x), sum(values<1 & lengths>1))
这是获取方块 "blocks" 和 "length" 的一种方法
indx <- inverse.rle(within.list(rle(x<1),
values <- seq_along(values)))[x<1]
split(x[x<1], indx)
#$`1`
#[1] 0.2 0.3
#$`3`
#[1] 0.2595997 0.2305125 0.3396233
length(with(rle(x <1), lengths[values]))
#[1] 2
或者
with(rle(x<1), sum(values & lengths>0) )
#[1] 2
如果是>5
,只需将lengths>0
替换为lengths>5
with(rle(x<1), sum(values & lengths>5) )
#[1] 0
我问过类似的问题
Block #1: 0.2,0.3
Block #2: 0.25959969,0.23051247,0.33962332
但是,只有当我说 values==0
时,该解决方案才有效。如果我使用小于号,即 values<1
,则答案不正确 (
这里我提供了可重现的例子:
x <- c(0.2,0.3,9.36401452,7.55982857,6.06992455,4.67168492,3.50976922,2.18683333,1.36610395,0.25959969,0.23051247,0.33962332)
with(rle(x), sum(values<1 & lengths>1))
这是获取方块 "blocks" 和 "length" 的一种方法
indx <- inverse.rle(within.list(rle(x<1),
values <- seq_along(values)))[x<1]
split(x[x<1], indx)
#$`1`
#[1] 0.2 0.3
#$`3`
#[1] 0.2595997 0.2305125 0.3396233
length(with(rle(x <1), lengths[values]))
#[1] 2
或者
with(rle(x<1), sum(values & lengths>0) )
#[1] 2
如果是>5
,只需将lengths>0
替换为lengths>5
with(rle(x<1), sum(values & lengths>5) )
#[1] 0