0

My data have the following form:

data

I want to create a loop that every time will take the observations from the beginning till the next 15 minutes, then after the first 15 minutes to additional 15 minutes, etc and will calculate the mean of the values for each sensor. I want to find the difference in the change in mean values per 15 minutes per sensor. I have set the date as

 dtm <- strptime("21/09/2015 10:41:00", format = "%d/%m/%Y %H:%M:%S",
                 tz = "CET")     

and the function for the minutes.

mns <- function(m) {x <- m * 60 return(x)}    

quart=rep(NA,nrow(data))
mean.min=rep(NA,nrow(data))
diff.min=rep(NA,nrow(data))
for (i in 0:4){
quart[i] <- data[data$Date >= dtm+mns(15)*i & data$Date <= dtm+mns(15)*(i+1),]
data$mean.min[i]<-aggregate(quart[i]$Value~quart[i]$SensorId, FUN=mean)
data$diff.min[i+1]<-rowMeans(abs(data$mean.min[i+1]-data$mean.min[i]),na.rm=T)}
lmo
  • 37,904
  • 9
  • 56
  • 69
Sigh
  • 13
  • 2
  • Hi, in order to write a good question with reproducible errors which will get you upvotes and answers, please include code that we can copy and paste to reproduce your data (or similar built-in / made-up data) and reproduce your problem. Please read: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jun 12 '16 at 21:23

2 Answers2

1

Consider this wordy base solution with sapply() which combines running SensorId group counts and running 15 minutes counts. Because of its nature, average is calculated as sum / count.

data$run15minavg <- sapply(1:nrow(data), function(i) {
                sum(((data[1:i, c("Date")] >= (data$Date[i] - as.difftime(15, units="mins")))
                      & (data[1:i, c("Date")] <= (data$Date[i]))
                      & (data[1:i, c("SensorId")] == data$SensorId[i]))
                   *  data[1:i,]$Value) /
                sum((data[1:i, c("Date")] >= (data$Date[i] - as.difftime(15, units="mins")))
                      & (data[1:i, c("Date")] <= (data$Date[i]))
                      & (data[1:i, c("SensorId")] == data$SensorId[i]))
                }    
             )
Parfait
  • 104,375
  • 17
  • 94
  • 125
0

The cut command and the dplyr package makes this a simple operation:

#generate sample data
date<-seq(from= as.POSIXct("2016-06-01"), by="1 sec", length.out = 1000)
sensorid<-sample(c("A sensor", "B sensor", "C sensor"), 1000, replace = TRUE )
value<-rnorm(1000, 50)
df<-data.frame(date, sensorid, value)


#group the results by sensor and 15 minute intervals
library(dplyr)
summarize(group_by(df, cut(df$date, breaks="15 min"), sensorid), mean(value))
Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • Correct me if I am wrong but OP wanted a continuous rolling average 15 mins. What about after the first 15 min break? – Parfait Jun 13 '16 at 00:30