Data formatting and preprocessing

From SourceWiki
Revision as of 13:34, 4 June 2008 by Wbuytaert (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


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
       }
   }