toro
toro

Reputation: 21

Count occurrences of streaks in columns

The dataframe consists of two columns:

df <- data.frame(Color=c('black','black','black','red','red', 'black','red','red', 'red','black','red','red', 'red','red'),
             Numbers=c(1,1,1,0,1,0,1,1,1,1,0,1,1,1))

How to count numbers of streaks in columns?

The result must be:

occurrences <- data.frame(Color=c('black','black','black','red','red', 'red'),
             Streaks=c(3,0,1,0,1,3),
             Count=c(1,1,1,1,1,2))

Streaks - how many times number 1 occurs in the column together (for every Color).

Count - frequency of Streaks.

Upvotes: 1

Views: 492

Answers (1)

A. Suliman
A. Suliman

Reputation: 13125

Here is one option using dplyr and data.table::rleid

library(dplyr)
df %>% group_by(Color, gr=data.table::rleid(Color, Number)) %>% 
       mutate(Streaks=n()) %>% 
       slice(1) %>% 
       group_by(Color, Number, Streaks) %>% 
       summarise(Count=n())

# A tibble: 6 x 4
# Groups:   Color, Number [?]
     Color Number Streaks Count
     <fct>  <dbl>   <int> <int>
  1 black      0       1     1
  2 black      1       1     1
  3 black      1       3     1
  4 red        0       1     2
  5 red        1       1     1
  6 red        1       3     2

Upvotes: 0

Related Questions