使用R中的for循环计算数据集的欧式距离

时间:2019-07-01 05:14:29

标签: r

我有一个包含5列和大量读数的数据集。看起来像这样:

Dataset Picture

现在,我要一次从该数据集中获取12个点,并计算从该数据集中的这12个点中的每个点到已经计算并存储的另一组值的欧几里得距离。在执行此操作时,我希望根据比较结果了解它属于哪个集群。我正在使用的代码如下:

DataNew <- read.csv("TeD.csv", header = TRUE, sep = ",")

a = 1
b = 12

DataNew$CCD <- (max(DataNew$CCD) - DataNew$CCD)/(max(DataNew$CCD) - 
min(DataNew$CCD))
DataNew$CCR <- (max(DataNew$CCR) - DataNew$CCR)/(max(DataNew$CCR) - 
min(DataNew$CCR))
DataNew$CCA <- (max(DataNew$CCA) - DataNew$CCA)/(max(DataNew$CCA) - 
min(DataNew$CCA))
DataNew$CCN <- (max(DataNew$CCN) - DataNew$CCN)/(max(DataNew$CCN) - 
min(DataNew$CCN))

write.table(DataNew, file = "DataNew.csv", sep = ",", col.names = F, append 
= T)

for (i in DataNew$Serial) {

DatNew = DataNew[a:b,4:6]

for (j in 1:12) {

     EuclDistMean1 = sqrt((DatNew$CCD[j] - CentMean[1][1])^2 + ( 
     DatNew$CCR[j] - CentMean[1][2])^2 + (DatNew$CCA[j] - CentMean[1][3])^3)

     write.table(EuclDistMean1, file = "Euclidian Distance 1.csv", sep = 
     ",", col.names = F, append = T)

     EuclDistMean2 = sqrt((DatNew$CCD[j] - CentMean[2][1])^2 + 
     (DatNew$CCR[j] - CentMean[2][2])^2 + (DatNew$CCA[j] - CentMean[2] 
     [3])^3)

     write.table(CentEntropy, file = "Euclidian Distance 2.csv", sep = ",", 
     col.names = F, append = T)

    }

    if(EuclDistMean1 < EuclDistMean2)
    {
        print("Cluster 1")
    }

   else if(EuclDistMean2 < EuclDistMean1)
   {
        print("Cluster 2")
   }

  a = a + 12
  b = b + 13
}

但是,当我执行此代码时,我得到了N / A值,并且在迭代了前12次之后程序中断了。如果有人可以建议对代码进行任何更新或更正,那么我将不胜感激。谢谢!

0 个答案:

没有答案