Ramp up/down missing time-series data in R -


i have set of time-series data (gps speed data, specifically), includes gaps of missing values signal lost. missing periods of short durations fill using na.spline, inappropriate longer time periods. ramp values last true value down zero, based on predefined acceleration limits.

#create sample data frame test <- as.data.frame(c(6,5.7,5.4,5.14,4.89,4.64,4.41,4.19,na,na,na,na,na,na,na,na,na,na,na,na,na,na,na,5,5.1,5.3,5.4,5.5)) names(test)[1] <- "speed"  #set rate of acceleration ramp ramp <- 6  #set sampling rate of receiver hz <- 1/10 

so missing data ramp use previous value , rate of acceleration next data point, until speed reached 0 (i.e. last speed [4.19] + (hz * ramp)), yielding following values:

3.59 2.99 2.39 1.79 1.19 0.59 0 

lastly, need in reverse fashion, ramp 0 when signal picks again.

hope clear.

cheers

it's not elegant, can in loop.

na.pos <- which(is.na(test$speed))  acc = false (i in na.pos) {     if (acc) {         speed <- test$speed[i-1]+(hz*ramp)     }     else {         speed <- test$speed[i-1]-(hz*ramp)         if (round(speed,1) < 0) {             acc <- true             speed <- test$speed[i-1]+(hz*ramp)         }      }     test[i,] <- speed } 

the result is:

   speed 1   6.00 2   5.70 3   5.40 4   5.14 5   4.89 6   4.64 7   4.41 8   4.19 9   3.59 10  2.99 11  2.39 12  1.79 13  1.19 14  0.59 15 -0.01 16  0.59 17  1.19 18  1.79 19  2.39 20  2.99 21  3.59 22  4.19 23  4.79 24  5.00 25  5.10 26  5.30 27  5.40 28  5.50 

note '-0.01', because 0.59-(6*10) -0.01, not 0. can round later, decided not to.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -