How to calculate interrupted datasets?

In my dataset, an energy meter has added up the values.
Unfortunately, when it is switched off, it starts again at zero. in this case, the correct


sum is therefore 2624+665=3288 W/h

Ideas /Methods on how to automatically calculate interrupted measurements over a period of time?
Any R Package ?

Many thanks in advance :sweat_smile:

1 Like

Hard to be very specific with just the screenshots. See the FAQ: How to do a minimal reproducible example reprex for beginners. It looks like you want to create a column that contains the first series as is, and continues with cumsum lagging by 1 period.

1 Like

cumsum seems indeed the right way to go
I created a simple example, imagining that a counter can reboot to 0 several times:

suppressPackageStartupMessages(library(tidyverse))
kwcount <- c(0, 2, 4, 6, 8, 0, 2, 3, 12, 0, 0, 5, 9, 12, 16, 20, 23, 0, 0, 3)
(df <- enframe(kwcount) %>% rename(c("ID" = "name", "kwcount" = "value")))

Then find difference between 2 measurements (this is why we need kwcount_lag), and apply cumsum.

df %>% mutate(
  kwcount_lag = lag(kwcount, default = 0), 
  kwcount_diff = ifelse(kwcount - kwcount_lag >= 0, kwcount - kwcount_lag, kwcount),
  kwcount_real = cumsum(kwcount_diff)
  )
2 Likes

What can I say? You have brought it precisely to the point.
I am thrilled and have already implemented your code.
Many thanks xvalda :blush: :blush:

Now I don't have to repeat or manually correct measurement series where my wife switches off the energy counter. :joy:

Glad it helped! Now you can have fun switching the counter on and off :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.