I asked similar question here . I want to find the number of blocks when the value is less than 1. In the below given example, the answer must be 2.
Block #1: 0.2,0.3
Block #2: 0.25959969,0.23051247,0.33962332
However, the solution works only if I say values==0
. If I use less than sign, ie values<1
, then the answer is not correct ( 1 0), while it must be 2.
Here I provide the reproducible example:
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))
Here is one way to get the "blocks" and the "length" of blocks
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
Or
with(rle(x<1), sum(values & lengths>0) )
#[1] 2
If it is >5
, just replace lengths>0
by lengths>5
with(rle(x<1), sum(values & lengths>5) )
#[1] 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.