Home > Enterprise >  How to select the window of rows which sum to a minimum value in each group and plot in R?
How to select the window of rows which sum to a minimum value in each group and plot in R?

Time:11-03

I want to divide a column in a df into groups and run something like rollsum() for each 36 sequential rows in a column (Online_h) inside each group and select those rows, where the sum is the minimum inside the group (compared to the sum of the rest again inside the group). It means that for each group I should get 36 rows summing as minimum compared to others.

My dataframe includes three columns of "Date", "Online_h" and "week". Column "week" is used to group the data by. Rows summing to minimum for each 36 sequential row should be calculated on the values in "Online_h".

The df looks like this: enter image description here


Data for 2nd example

set.seed(2021)
Date <- paste(Sys.Date() - 4*7, "23:00:00")
Date <- as.POSIXct(Date)
Date <- seq(Date, length.out = 4*7*24, by = "1 hour")
week <- as.integer(format(Date, "%V"))
df <- data.frame(Date,
                 Online_h = sample(50, length(week), TRUE),
                 week)
  • Related