0

I have the following data set.

dput(s1[1:10,])

structure(list(V1 = c(1, 1, 1, 5, 5, 2, 2, 3, 1, 1), V2 = c(0, 
0.55, 0.9, 3.125, 5, 19.96666667, 12.25, 35.15, 5.4, 58.58)), 
na.action = structure(260:270, .Names = c("260", 
"261", "262", "263", "264", "265", "266", "267", "268", "269", 
"270"), class = "omit"), row.names = c("33", "317", "6", 
"202","250", "185", "28", "251", "218", "116"), class = 
"data.frame")

I would like to get one or more values satisfying the following conditions:

[1] count (the sum) the number of consecutive occurrences when V2 (the second column) is below 1 for at least 3 consecutive time steps AND the value of V1 is <= 4 for all time steps.

[Expected Output]

In the above example, the answer should be 1, which corresponds to the three four timesteps. There is only one case that satisfied the criteria in the above example.

Here's another case:

structure(list(V1 = c(1, 1, 1, 5, 5, 2, 2, 3, 1, 1), V2 = c(0, 
0.55, 0.9, 3.125, 5, 19.96666667, 12.25, 0.1, 0.3, 0.5)), 
na.action = structure(260:270, .Names = c("260", 
"261", "262", "263", "264", "265", "266", "267", "268", "269", 
"270"), class = "omit"), row.names = c("33", "317", "6", 
"202","250", "185", "28", "251", "218", "116"), class = 
"data.frame")

In this case the answer should be 2 because of the three time steps.

What I have so far:

I was able to count the consecutive occurrences using the rle() function:

with(rle(s1$V2 < 1), sum(lengths[values] >= 3))

but how do I add the other condition here (V1 should be <=4 for all time steps.

Any idea on how I can apply this in R?

Lyndz
  • 347
  • 1
  • 13
  • 30
  • 1
    Can you list out some more of your expected output? Can you just make a new vector of your logical conditions (e.g. `cond <- s1$V2<1 & s1$V1<4`) and sub that into your `rle` call? – Brian Apr 24 '20 at 17:44
  • Hi @Brian, I modified my post and added the expected output. I tried this `sum(with(rle(dat2$V2 < 1), sum(lengths[values] >= 3)) & with(rle(dat2$V1 <= 4), sum(lengths[values]>=3)))`. but it doesn't work when I apply to the second case. It is always giving me a sum of 1. – Lyndz Apr 24 '20 at 19:32
  • 1
    @Brian meant something like this: f = function(x){cond = x$V2 < 1 & s1$V1 < 4 ; with(rle(cond),sum(lengths[values]>=3))} – StupidWolf Apr 24 '20 at 20:24
  • 1
    f(s1) gives me 2, and if i do s2 as your second data.frame, f(s2) gives me 2 – StupidWolf Apr 24 '20 at 20:25
  • @StupidWolf..Thank you for this!. I got what you mean.. I have another question in your response on my other post. https://stats.stackexchange.com/questions/462314/help-on-a-simple-monte-carlo-significance-test-probability-of-occurrence/462636#462636..Thank you so much for the help. – Lyndz Apr 25 '20 at 06:36

0 Answers0