mardi 23 juin 2020

Computing values of each row of a data.frame using linear interpolation in R?

I would like to compute capacity corresponding to its Level of the ElevationData data.frame. I do have ElevationStorage data.frame where i have standard Elevation and Storage capacity curve data. In case any Level value from ElevationData equals to any Elevation value of ElevationStorage, i would use its corresponding Storage value otherwise i need to interpolate between Level and Elevation data and use an interpolated Storage value. Here is my incomplete code as i ran out of ideas to provide a complete one

library(tidyverse)
library(lubridate)

ElevationData <- data.frame(Date = seq(as.Date("2010-01-01"), to = as.Date("2010-01-31"), by = "days"),
                         Level = runif(31,364.1,364.7),
                         Outflow = runif(31,1,25))
ElvationStorage <- data.frame(Elevation = c(362.5,363.0,363.5,364.0,364.5,365.0,365.5,366.0),
                              Stroage = c(1000,2500,3500,4500,6000,7500,9000,10000))
ElevationData$Capacity <- if (ElevationData$Level == ElvationStorage$Elevation){
  Capacity = ElvationStorage$Stroage
} else
  # linear interpolation
  Capacity = 
  }

Output:

Here is a snapshot of the linear interpolation equation- i guess r has a built in function for this (approx()). enter image description here

Aucun commentaire:

Enregistrer un commentaire