首页 > 解决方案 > 如何迭代我的向量中的所有元素以进行反距离加权

问题描述

我有一个看起来像这样的数据框:

  Teff logg M_div_H       U       B      V      R      I     J     H     K     L Lprime     M
1 2000  4.0    -0.1 -13.443 -11.390 -7.895 -4.464 -1.831 1.666 3.511 2.701 4.345  4.765 5.680
2 2000  4.5    -0.1 -13.402 -11.416 -7.896 -4.454 -1.794 1.664 3.503 2.728 4.352  4.772 5.687
3 2000  5.0    -0.1 -13.358 -11.428 -7.888 -4.431 -1.738 1.664 3.488 2.753 4.361  4.779 5.685
4 2000  5.5    -0.1 -13.220 -11.079 -7.377 -4.136 -1.483 1.656 3.418 2.759 4.355  4.753 5.638
5 2200  3.5    -0.1 -11.866  -9.557 -6.378 -3.612 -1.185 1.892 3.294 2.608 3.929  4.289 4.842
6 2200  4.5    -0.1 -11.845  -9.643 -6.348 -3.589 -1.132 1.874 3.310 2.648 3.947  4.305 4.939
...

这是完整数据框的链接:https ://www.dropbox.com/s/prbceabxmd25etx/lcb98cor.dat?dl=0

我是这样读这张表的:

library(data.table) 
d <- fread("https://www.dropbox.com/s/prbceabxmd25etx/lcb98cor.dat?dl=1")
setnames(d,"#Teff","Teff")

假设我有两个值:

input_Teff = 4.8529282904170595E+003
input_log_g = 1.9241934741026787E+000

请注意每个V值如何具有唯一的Teff,logg组合。从输入值中,我想为V. 这是我使用gstat包进行插值的方法。

# fit model
idw <- gstat(id="V", formula = V~1, locations = ~Teff+logg, data=d, nmax=7, set=list(idp = .5))
        
# new "points" to predict to 
newd <- data.frame(Teff=c(4100, input_Teff), logg=c(1.5, input_log_g))
        
p <- predict(idw, newd)
# inverse distance weighted interpolation
BC.lambda <- p$V.pred[2]

问题是,我不仅要为 V 插入值,还要为这个向量中的所有元素插入值,一次一个:

lambdas <- c('U','B','V','R','I','J','H','K','L','Lprime','M')

我将迭代lambdas执行此操作,有人可以帮助我完成我必须在这段代码中进行的更改吗?

  # fit model
        idw <- gstat(id=lambdas[i], formula = V~1, locations = ~Teff+logg, data=d, nmax=7, set=list(idp = .5))
        
        # new "points" to predict to 
        newd <- data.frame(Teff=c(4100, input_Teff), logg=c(1.5, input_log_g))
        
        z2 = capture.output(p <- predict(idw, newd))
        # inverse distance weighted interpolation
        BC.lambda <- p$V.pred[2]

标签: r

解决方案


推荐阅读