Search code examples
rplotalgebra

calculating Y value given X using slope from plot/datapoints


I'm trying to figure out what the easiest way of doing this might be. I could have various data-table lengths and I want to automatically fill in the missing values with the slope of the connecting datapoints. I'm just not sure the easiest way to do this programmatically.

I imagine there is some sort of thing like this that I could do but again I don't know how to apply this or if there is an easy loop/dplyr/ other function that solves this problem.

Basically if there was a line plot, where would each of the points be as the line crosses over that X/index value? thats what I want.

        lm(y~index)$coef[[2]] * index + lm(y~index)$coef[[1]]

Two examples:

        df = data.frame(index = 1:6, y = c(10,NA,20,NA,NA,2) 
        df2 = data.frame(index = 1:8, y=c(NA,NA,2,NA,NA,NA,NA,18)

     solutions:
 df = data.frame(index = 1:6, y = c(10,15, 20,14,8,2)#slope of 5 and then -6
 df2 = data.frame(index = 1:8, y=c(NA,NA,2,5.2,8.4,11.6,14.8,18) #slope of 3.2

Solution

  • You want to use approx along with the argument n specifying how many data points linear interpolations will occur. It requires, as you have done, that there are least two complete cases of (x,y) pairs.

    data.frame(approx(df, n = nrow(df)))
    
      x  y
    1 1 10
    2 2 15
    3 3 20
    4 4 14
    5 5  8
    6 6  2