I have a table like this, which is flyTracesFiltered.
Time Right Left
1 0.000000000 18.21980 30.98789
2 0.009222031 22.15157 37.18590
3 0.022511959 25.63218 42.49231
4 0.029854059 28.43851 46.57811
5 0.039320946 30.43885 49.29414
6 0.052499056 31.60561 50.67852
What I wanted to do is to downsample by time. That is, I want to average all the values within a certain period to decrease the number of samples. In my case I was using averages of 0.05 seconds averages (20Hz). The function I did look like this:
flyDataDownsampleTime <- function(flyTracesFiltered, samplePeriod) {
AvgRight<-NULL
FlyDataRight<-NULL
AvgLeftt<-NULL
FlyDataLeft<-NULL
AvgTime<-NULL
FlyDataTime<-NULL
for (i in seq(0,ceiling(max(flyTracesFiltered$Time)),samplePeriod)){
AvgRight <-mean(flyTracesFiltered$Right[flyTracesFiltered$Time>=i & flyTracesFiltered$Time <= (i+samplePeriod)])
FlyDataRight<-c(FlyDataRight,AvgRight)
AvgLeft <-mean(flyTracesFiltered$Left[flyTracesFiltered$Time>=i & flyTracesFiltered$Time <= (i+samplePeriod)])
FlyDataLeft<-c(FlyDataLeft,AvgLeft)
AvgTime <-mean(flyTracesFiltered$Time[flyTracesFiltered$Time>=i & flyTracesFiltered$Time <= (i+samplePeriod)])
FlyDataTime<-c(FlyDataTime,AvgTime)
}
flyTracesDownTime <- data.frame("Time" = FlyDataTime, "Right" = FlyDataRight, "Left" = FlyDataLeft)
return(flyTracesDownTime)
}
I wanted to ask if there is a way to improve this because it takes very long for large dataframes. I have problems to implement the apply family functions when I need iterations like in this case (because of indexing). I also read about the Vectorize function but I dont know if this could make the code more effective. Any suggestions?