brnwa
brnwa

Reputation: 25

Count negatives if at least six consecutive negative values in a column

I have a data.frame column with hourly values and want to count all negative values, if they are in a sequence of at least six consecutive negatives.

Example:

df <- data.frame(Values=c(-2, 2, 3, 4,-1,-1,-2,-3,
                          -1,-1,-1, 5, 4, 2,-4,-2,
                          -3,-4,-1, 3, 4, 4,-3,-1,
                          -2,-2,-3,-4))

The expected result would be 13, since the middle sequence of consecutive negatives contains only five negatives and is thus not counted.

Upvotes: 0

Views: 593

Answers (3)

Rich Scriven
Rich Scriven

Reputation: 99331

You could use the base function rle() along with sign(). The sign() function converts negatives and positives to -1 and 1, respectively. This makes a nice vector to pass to rle() to get the run-lengths. Then we can subset the run-lengths with the desired condition and take the sum.

with(rle(sign(df$Values)), sum(lengths[lengths >= 6 & values < 0]))
# [1] 13

Upvotes: 3

gisi
gisi

Reputation: 1

you can always define your own function and call it.

NegativeValues <- function(x) {
   count <- 0
   innercount <- 0
   for (i in c(x, 0)) {
      if (i < 0) {
         innercount <- innercount + 1
      }
      else {
         if (innercount >= 6)
            count <- count + innercount
         innercount <- 0
      }      
   }
   return(count)
}

NegativeValues(df$Values)

Upvotes: 0

DatamineR
DatamineR

Reputation: 9618

Try:

library(cgwtools)
res <- seqle(which(df<0))
sum(res$lengths[res$lengths>=6])
[1] 13

Upvotes: 2

Related Questions