1

I have data like following:

    time:                   a:
    2016-02-01 16:00:00      111

The value of column "a" could be any number and NA. For each "time" number/NA is assigned to "a". The data is ordered by "time" ascending. My task is to aggregate periods when the "a" equals NA (R language). Examplary data set:

     2016-02-01 16:00:00 NA
     2016-02-01 16:00:01 NA
     2016-02-01 16:00:02 NA
     2016-02-01 16:00:03 NA
     2016-02-01 16:00:04 21

So there are 4 "NA" values in a row and it takes 4 seconds. So it should be aggregate to interval 0 - 15 sec. The original dataset has about 100000 records and each interval of NA values should be aggregated in the same way. Intervals are not equal, it starts with 0-15 sec and ends with 1-2 days.

What is appropriate way to do this?

Michał
  • 123
  • 1
  • 1
  • 11
  • What do you want the output to look like? `rle` will be useful. – alistaire Mar 30 '16 at 17:12
  • the output should be like: `Interval` and `count` as headers, and then in column with intervals something like (0, 15s), .... [1d, 2) and in column with counts something like 123. I thought about knith::kable. I have to display result with the use of Markdown template. I'm completly new with R so I would be really thankful for help – Michał Mar 30 '16 at 17:30
  • You need to edit with [a minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Without seeing enough of what you have and what you want, it's impossible to provide a thorough answer. It probably only needs `rle` and maybe `lubridate`'s `interval` object type, but it could be structured in any number of ways. – alistaire Mar 30 '16 at 17:35
  • One could try the cut command: cut(df$time, breaks= "15 sec"). See help for "cut.POSIXt". This could bin the data into the desired time intervals. – Dave2e Mar 30 '16 at 18:15

0 Answers0