Search code examples
rfor-loopinterpolationcalibration

Adding interpolated model data column to field dataset in r


I'm trying to compare my model data with field data in r. Field data is at certain depths(1,20,50,75,100,150..). Model data output is not at the same depths and have to be interpolated(linear) and preferably added to a new column in the field data.

I would like to run a for loop on each row of the model data and use the approx function to interpolate data to fit the depths of the field data and add it to the model_chla column. I have extracted the same dates(19 dates) from both data sets, so only interpolation on model depth is necesarry.

chla_FIELD         # some of the field data with the empty model column
          date depth  chla model_chla
1   2008-02-21     1  0.06         NA
2   2008-02-21    20  0.05         NA
3   2008-02-21    50  0.03         NA
4   2008-02-27     1  0.08         NA
5   2008-02-27    20  0.04         NA
6   2008-02-27    50  0.03         NA
7   2008-02-27    75  0.03         NA
8   2008-02-27   100  0.01         NA
9   2008-03-07     1  0.07         NA
10  2008-03-07    20  0.05         NA
11  2008-03-07    50  0.03         NA
12  2008-03-07    75  0.02         NA
13  2008-03-07   100  0.02         NA
14  2008-03-07   150  0.01         NA


chla_MODEL          # Some of the model data to be interpolated to field data depths and added to column
             dt depth        chla
766  2008-02-21   1.0  0.22385520
767  2008-02-21   7.5  0.21676594
768  2008-02-21  15.0  0.19189246
769  2008-02-21  25.0  0.15524526
770  2008-02-21  40.0  0.14638090
771  2008-02-21  62.5  0.14301939
772  2008-02-21  87.5  0.14094244
773  2008-02-21 112.5  0.13897014
774  2008-02-21 137.5  0.13680272
775  2008-02-21 162.5  0.13430916
776  2008-02-21 187.5  0.13133907
777  2008-02-21 212.5  0.12757768
778  2008-02-21 237.5  0.12237051
779  2008-02-21 262.5  0.11396441
780  2008-02-21 287.5  0.09206185
856  2008-02-27   1.0  0.24240938
857  2008-02-27   7.5  0.23447734
858  2008-02-27  15.0  0.21238998
859  2008-02-27  25.0  0.15545718
860  2008-02-27  40.0  0.14592259
861  2008-02-27  62.5  0.14171122
862  2008-02-27  87.5  0.13900438
863  2008-02-27 112.5  0.13662824

My start is something like this, but I'm new to the approx function and adding data to columns in loops.

chla_approx <- function(FieldDATA, ModelDATA)
  
  for (row in FieldDATA) {
  
  }

Or is there an easier way to do this? Thanks!


Solution

  • The following code assumes that all dates in chla_FIELD are present in chla_MODEL.

    If the dates in the data sets are already of class "Date" these two instructions are not needed.

    chla_FIELD$date <- as.Date(chla_FIELD$date)
    chla_MODEL$dt <- as.Date(chla_MODEL$dt)
    

    Now split/apply/combine.

    sp_field <- split(chla_FIELD, chla_FIELD$date)
    sp_model <- split(chla_MODEL, chla_MODEL$dt)
    
    approx_fun_list <- lapply(sp_model, function(data){
      approxfun(data[["depth"]], data[["chla"]])
    })
    
    chla_FIELD <- lapply(names(sp_field), function(nm){
      f <- approx_fun_list[[nm]]
      sp_field[[nm]]$model_chla <- f(sp_field[[nm]]$depth)
      sp_field[[nm]]
    })
    chla_FIELD <- do.call(rbind, chla_FIELD)
    

    Final cleanup.

    rm(sp_field, sp_model)