Difference between revisions of "Data formatting and preprocessing"

From SourceWiki
Jump to navigation Jump to search
m
 
 
Line 6: Line 6:
 
==Linear interpolation==
 
==Linear interpolation==
  
Remove (short) gaps in a timeseries by linear interpolation:
+
Remove (short) gaps in a timeseries by linear interpolation. Make sure your last value is not an NA because then the while loop runs forever.
  
 
     for(i in 1:length(timeseries)) {
 
     for(i in 1:length(timeseries)) {

Latest revision as of 13:34, 4 June 2008


These are quick and dirty scripts that often are not very efficient but do their job.

Linear interpolation

Remove (short) gaps in a timeseries by linear interpolation. Make sure your last value is not an NA because then the while loop runs forever.

   for(i in 1:length(timeseries)) {
       if(is.na(timeseries[i])) {
           j <- 1
           while(is.na(timeseries[i+j])) j <- j+1
           delta <- (timeseries[i+j] - timeseries[i-1]) / (j+1)
           timeseries[i] <- timeseries[i - 1] + delta
       }
   }