Data formatting and preprocessing

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