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?