1

I want to calculate 1-min and 5-min mean values from a dataset with 1-min values.

The dataset looks like this:

     time     BC
1  10:40:01  2217
2  10:40:02  5776
3  10:40:03  2111
4  10:40:04  1407
5  10:40:05  2056
.
.
.
n  11:30:58  6044

I found a post where someone suggested the zoo(aggregate) function for this.

I tried it like this:

time<-as.character(time)
BC<-as.numeric(BC)
BC_mean<-aggregate(BC, trunc(time, "00:01:00"), mean)

However, it doesn't work. I am not quite sure if I understand the aggregate function correctly and the definitions I found online are confusing me even more. Can someone help me?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
brtstph
  • 33
  • 4

1 Answers1

0

We can convert the 'Time' to POSIXct class, and use cut to create the groups to be used in the aggregate

brk = "5 min"
df2 <- transform(df1, timeGrp = cut(as.POSIXct(time, format = "%H:%M:%S"), breaks = brk))
aggregate(BC~timeGrp, df2, mean)
akrun
  • 874,273
  • 37
  • 540
  • 662