chiser
chiser

Reputation: 161

Optimizing the code for downsampling

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?

Upvotes: 4

Views: 297

Answers (1)

HoneyBadger
HoneyBadger

Reputation: 108

You can create a new field, say, group_num

flyTracesFiltered$group_num <- floor(flyTracesFiltered$Time/0.05)

Where 0.05 period is the time-space you want to shorten your sample into. Aggregate over this field like-

flyTracesFiltered2 = aggregate(flyTracesFiltered$Right,
                     list(group_num=flyTracesFiltered$group_num), mean)

This will give you a new data frame with averaged out value over each group_num.

You can do the same with other column (Left) and merge the data frames or just add the new column.

Upvotes: 2

Related Questions