Home » excel » r – Fill blank cells using linear interpolation

r – Fill blank cells using linear interpolation

Posted by: admin April 23, 2020 Leave a comment

Questions:

First I have to say that Excel is not my tool of trade, and I do this for people who are not R users. This was supposed be an easy task, but I am struggling to find a way to fill empty cells using linear interpolation. Suppose I have a following Excel sheet:

Row Date      Interpolated date
1   09/09/13    
2       
3   15/10/13    
4       
5       
6       
7       
8       
9       
10  03/04/14

Now I would like to copy the non-empty values from Date to Interpolated date and then fill up the empty cells by interpolating between adjacent non-empty cells. Note that the gap between empty values in Date column is haphazard. The result should look like this (this is done using na.approx function from zoo package for R):

Row Date      Interpolated date
1   09/09/13  09/09/13
2   <NA>      27/09/13
3   15/10/13  15/10/13
4   <NA>      08/11/13
5   <NA>      02/12/13
6   <NA>      26/12/13
7   <NA>      20/01/14
8   <NA>      13/02/14
9   <NA>      09/03/14
10  03/04/14  03/04/14

As far as I have understood, this is probably not possible using standard Excel functions and one should maybe use VBA macros. I am a complete newbie in VBA macros. I found a similar question and solution for it, but I do not manage to apply the solution to my Excel sheet. I would like Interpolated date column to update either automatically, when values are changed in Date column or at least a simple button, which a user could press to update the values. What would be the simplest way to do the interpolation in this case?

PS. Here is the R code, if that would help:

dat <- structure(list(Row = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Date = structure(c(15957, NA, 15993, NA, NA, NA, NA, NA, NA, 16163), class = "Date")), .Names = c("Row", "Date"), row.names = c(NA, -10L), class = "data.frame")

library(zoo)
dat$Interpolated.date <- as.Date(na.approx(dat$Date))
How to&Answers:

If you have two dates in a column separated by intervening blanks, select the cells (including the two filled cells) and run this tiny VBA macro:

Sub DateInterp()
    Selection.DataSeries Rowcol:=xlColumns, Type:=xlGrowth, Date:=xlDay, _
        Trend:=True
End Sub

Answer:

You could use an add-in such as http://www.srs1software.com/SRS1CubicSplineForExcel.aspx

You are looking for linear interpolation functions.