Hi again,
Thanks for providing the extra info. Here is another way you can solve this:
library(tidyverse)
#Get the data
myData = data.frame(
Teff = c(2000, 2000, 2000, 2000, 2200, 2200, 2200, 2500, 2500, 2500),
logg = c(4, 4.5, 5, 5.5, 3.5, 4.5, 5.5, -1.02, -0.7, -0.29),
M_div_H = c(-0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1, -0.1),
U = c(-13.443,-13.402,-13.358,-13.22,-11.866,
-11.845,-11.655,-7.41,-7.008,-6.526),
B = c(-11.39,-11.416,-11.428,-11.079,-9.557,
-9.643,-9.615,-7.624,-7.222,-6.74),
V = c(-7.895,-7.896,-7.888,-7.377,-6.378,
-6.348,-6.279,-6.204,-5.818,-5.357)
)
#Set the input values
input_Teff = 2300
input_log_g = 3.86
#Step 1 - Calculate the distance to the point of interest
myData = myData %>%
mutate(dist = sqrt((Teff - input_Teff)^2 + (logg - input_log_g)^2))
myData
#> Teff logg M_div_H U B V dist
#> 1 2000 4.00 -0.1 -13.443 -11.390 -7.895 300.0000
#> 2 2000 4.50 -0.1 -13.402 -11.416 -7.896 300.0007
#> 3 2000 5.00 -0.1 -13.358 -11.428 -7.888 300.0022
#> 4 2000 5.50 -0.1 -13.220 -11.079 -7.377 300.0045
#> 5 2200 3.50 -0.1 -11.866 -9.557 -6.378 100.0006
#> 6 2200 4.50 -0.1 -11.845 -9.643 -6.348 100.0020
#> 7 2200 5.50 -0.1 -11.655 -9.615 -6.279 100.0134
#> 8 2500 -1.02 -0.1 -7.410 -7.624 -6.204 200.0595
#> 9 2500 -0.70 -0.1 -7.008 -7.222 -5.818 200.0520
#> 10 2500 -0.29 -0.1 -6.526 -6.740 -5.357 200.0431
#Only keep the 4 best (closest) points
closestPoints = myData %>% top_n(desc(dist), n = 4)
closestPoints
#> Teff logg M_div_H U B V dist
#> 1 2200 3.50 -0.1 -11.866 -9.557 -6.378 100.0006
#> 2 2200 4.50 -0.1 -11.845 -9.643 -6.348 100.0020
#> 3 2200 5.50 -0.1 -11.655 -9.615 -6.279 100.0134
#> 4 2500 -0.29 -0.1 -6.526 -6.740 -5.357 200.0431
#Calculate the result for V according to formula
closestPoints %>%
summarise(result = sum(dist * V) / sum(dist))
#> result
#> 1 -5.943761
#If you like to calculate the result for all columns at once...
myFun = function(col, dist){sum(dist * col) / sum(dist)}
closestPoints %>%
summarise(across(c(-Teff, -logg, -M_div_H, -dist), myFun, dist = dist))
#> U B V
#> 1 -9.683393 -8.458889 -5.943761
Created on 2021-08-02 by the reprex package (v2.0.0)
You can change the input and start again from step 1 to generate new results
Hope this helps,
PJ