Use the smooth.spline function (set the smoothing spline)
mx=path.w.missing$x my=path.w.missing$y s<-smooth.spline(mx,my) plot(mx,my) lines(s)

predict(s,1:10)
And you can use a linear model to match it.
> summary(lm(my~mx)) Call: lm(formula = my ~ mx) Residuals: Min 1Q Median 3Q Max -12.772 -8.642 3.112 5.831 17.237 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -12.60098 3.46583 -3.636 0.000444 *** mx 0.56579 0.02138 26.469 < 2e-16 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 7.896 on 98 degrees of freedom Multiple R-squared: 0.8773, Adjusted R-squared: 0.876 F-statistic: 700.6 on 1 and 98 DF, p-value: < 2.2e-16
as you can see, p <0.05, so my and mx can fit as a string:
my=-12.60098+0.56579*mx
ggplot2 can easily execute this line:
d=data.frame(mx,my) library(ggplot2) ggplot(d,aes(mx,my))+geom_point()+geom_smooth(method="lm")

Suppose that the line Ax+By+C=0 , and the point (X0,Y0) , indicate the distance of the line:
|AX0+BY0+C|/√(A²+B²)
therefore, in this case 0.56579 * mx-my-12.60098 = 0, A = 0.56579, B = -1, C = -12.60098, it is easy to calculate the distance from point to line and find the nearest point to the line.
In addition, if you want to find the nearest point, remove the denominator √ (A² + B²), this will not affect the sorting, therefore optimizing the formula:
|AX0+BY0+C|
Result
> for(i in 1:2503501){ + temp=abs(centers[[1]][i]*0.56579-centers[[2]][i]-12.60098) + if(m>temp){ + m=temp + pos=i + } + } > m [1] 2.523392e-05 > pos [1] 638133
Use rcpp to speed up the test.cpp program
#include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] int closest(NumericVector a, NumericVector b) { int length = a.size(); double temp,m=10000; int pos=0; for(int i=0;i<length;i++){ temp=a[i]*0.56579-b[i]-12.60098; if(temp>=0){ if(m>temp){m=temp;pos=i;} }else{ if(m>-temp){m=-temp;pos=i;} } } return pos+1; }
Result (Rcpp costs about 2 seconds):
> sourceCpp("test.cpp") > closest(centers[[1]],centers[[2]]) [1] 974698 > abs(centers[[1]][974698]*0.56579-centers[[2]][974698]-12.60098) [1] 0.0002597022