0

I have a table like this:

Time                   Indicator    Value
"2017-05-22 13:52:10"  1            10
"2017-05-22 13:52:12"  1            11
"2017-05-22 13:52:14"  1            9
"2017-05-22 13:52:16"  0            4
"2017-05-22 13:52:18"  0            10
"2017-05-22 13:52:20"  1            7
"2017-05-22 13:52:22"  1            9
"2017-05-22 13:52:24"  1            7

And I would like to compute three Values: The Means for all Rows, where Indicator is constant.

So I would like to do a group by Indicator for three Bins: The first Bin with Indicator=1, the second Bin with Indicator=0 and the third bin with Indicator=1. The basic idea is, that I want to create bins by the change of one variable. How can I do that?

Thanks for your Help!

schmitzi89
  • 47
  • 7
  • 3
    A good use case for `rle`, for example `with(rle(c(1,1,1,0,0,1,1,1)), rep(seq_along(values), lengths))` to create the groups – talat May 31 '17 at 12:20

1 Answers1

-3

We can use rleid from data.table

library(data.table)
setDT(df1)[, grp := rleid(Indicator)]

Or using dplyr

library(dplyr)
df1 %>% 
    mutate(grp = cumsum(c(TRUE, diff(Indicator)!=0)))
akrun
  • 874,273
  • 37
  • 540
  • 662