0

I have an R data frame which is just 1 row and looks like so:

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
 1  1  1  1  0  1  0  1  0  0

I want to know how many 1s there are in a row starting from the first position. So this would be 4.

More examples:

(Example 1)

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
 0  1  1  1  0  1  0  1  0  0

would be 0 because the first position isn't even a 1.

(Example 2)

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
 1  0  1  1  0  1  0  1  0  0

would be 1.

How can I implement this?

numInStreakAtBeginning = function(row) {

}

Additionally, how can I implement a method that looks for the largest streak regardless of where it starts? For example,

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
 1  0  1  1  1  1  0  1  0  0

would be 3.

Arun
  • 116,683
  • 26
  • 284
  • 387
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • 2
    Probably you want `?rle`. – joran May 07 '14 at 14:37
  • 1
    Right but how do I specify where the rle has to start and what value it has to take. Or furthermore, that it can start anywhere but has to have a certain value. – CodeGuy May 07 '14 at 14:50
  • You can infer all of that from the output of `rle`, which will count all runs of all values. – joran May 07 '14 at 14:51
  • 1
    First question: `r <- rle(row) ; ifelse(r$values[1] == 1, r$lengths[1], 0)`. Second question: `r <- rle(row) ; max(r$lengths[r$values == 1])`. – josliber May 07 '14 at 14:53
  • To be fair, the question was specific to 1's, and contingent upon the first value. I agree that knowing about `rle()` is the key hurdle to clear for this question, but there's somewhat more to the question. Furthermore, the question asks for a function that performs 2 tasks. This might seem like a simple question, but I see it as a spin-off of the "original" question, not a duplicate. – rbatt May 07 '14 at 15:01
  • for the first question: what about `min(which(x==0)-1)` where `x` is your data frame (if you have more rows you can use `min(which(x[1,]==0)-1)` ? – talat May 07 '14 at 15:01
  • @joran According to this post, http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled, wouldn't you say the current question is more "similar" or "related"? I'm pointing this out b/c my reading of the question, even knowing rle, was that it was worth answering. – rbatt May 07 '14 at 15:07
  • 1
    @rbatt The commands to get from the `rle` output to the final solution are short (each is a one-liner) and simple (`ifelse` and indexing one vector based on the values of another). Given the simplicity of answering the questions with the `rle` output, this seems like a duplicate to me. – josliber May 07 '14 at 15:17

1 Answers1

0

Function that returns a list of length 2, first element of the list is the length of the starting streak of 1's, the second element is the length of the longest streak of 1's.

test <- as.data.frame(matrix(rbinom(20, 1, 0.5), nrow=1))

numInStreakAtBeginning <- function(row){
    runs <- rle(row[1,]) # convert to vector
    retList <- list() # store output

    if(!runs$values[1]){ # if the first value is 0, the length of the first streak is 0
        retList$firstStreak <- 0
    }else{
        retList$firstStreak <- runs$lengths[1] # if the first value is not 0, return length of first streak
    }

    retList$longestStreak <- max(runs$lengths[runs$values==1L]) # the longest streak of 1's, regardless of starting point

    return(retList)
}

numInStreakAtBeginning(test)
rbatt
  • 4,677
  • 4
  • 23
  • 41