0

I am new to R and have never posted on this website before, so if I haven't provided enough information or if this is badly phrased, I apologize in advance.

I have a data frame that represents energy data every 5 minutes. To find the basal rate over the entire day, I want to sum every 5 values (for a 30-minute period) and find the lowest value every 5 minutes

So if I had (a, b, c, d, e, f, g, h, i) with each letter representing a data point, I would want it to find the sum of:

  1. (a, b, c, d, e, f)
  2. (b, c, d, e, f, g)
  3. (c, d, e, f, g, h)
  4. (d, e, f, g, h, i)

If the function can choose which of these 4 is the lowest, that would be perfect, but if it can just provide me sums in a new column, I could just sort that with another function.

My data looks like this (but a lot longer, not sure if I should post the whole thing):

Time Value
1:00 5.5
1:05 5.3
1:10 5.4
1:15 6.3
1:20 4.6
1:25 6.4
qsxsqsxs
  • 161
  • 1
  • 8

2 Answers2

0

Have a look at the rollmean function in the zoo library, eg:

data <- data %>%
  mutate(Avg_5period=zoo::rollmean(Value,k=5,fill=NA))
Emanuel V
  • 143
  • 8
0

You can use rollsum function from zoo to calculate rolling sum. Based on where you want to put the answer you can align the output accordingly.

For example, right align - when you want to sum of position 1, 2, 3, 4 and 5 should be put at position 5.

library(dplyr)
library(zoo)

df %>% mutate(roll_sum = rollsumr(Value, 5, fill = NA))

#  Time Value roll_sum
#1 1:00   5.5       NA
#2 1:05   5.3       NA
#3 1:10   5.4       NA
#4 1:15   6.3       NA
#5 1:20   4.6     27.1
#6 1:25   6.4     28.0

Left align - when you want to put sum of position 1 to 5 at position 1.

df %>% mutate(roll_sum = rollsum(Value, 5, fill = NA, align = 'left'))

#  Time Value roll_sum
#1 1:00   5.5     27.1
#2 1:05   5.3     28.0
#3 1:10   5.4       NA
#4 1:15   6.3       NA
#5 1:20   4.6       NA
#6 1:25   6.4       NA

Center align - When you want to sum of position 1 to 5 to be included at position 3.

df %>% mutate(roll_sum = rollsum(Value, 5, fill = NA))

#  Time Value roll_sum
#1 1:00   5.5       NA
#2 1:05   5.3       NA
#3 1:10   5.4     27.1
#4 1:15   6.3     28.0
#5 1:20   4.6       NA
#6 1:25   6.4       NA

Note that in all the case the numbers are the same but the position at which answers are placed differs. If you store the output of above operation in result, you can get minimum value by doing min(result$roll_sum, na.rm = TRUE).

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213