R 中未调用的函数

the function not called in R

我写了一个dbscan算法的代码。 当我在 main 中调用函数时 它不起作用,我不知道为什么

这是代码

x=read.delim("C:/Users/mf/Desktop/stp.txt")
y=read.delim("C:/Users/mf/Desktop/stp.txt")

hash=0
c=temp1=0
q=1
C=0
eps=30
MinPts=30

lable=matrix(-2,1,nrow(x))
clusterlab=matrix(-3,1,nrow(x))


for(p in 1:nrow(x))
{
  if(lable[p]==-2)
{
  lable[p]=1 #visited=1 and nonvisited=-2
  NeighborPts = regionQuery(p, eps)

  temp=nrow(NeighborPts)-1

    if (temp < MinPts){

           clusterlab[p]=0 #noise = 0


   }
  else if(temp>=MinPts){

          C = C+1
          haha=expandCluster(p, NeighborPts, C, eps, MinPts,hash,clusterlab,lable)

    }

  }
}




expandCluster <- function(p, NeighborPts, C, eps, MinPts,hash,clusterlab,lable) {
  hash=hash+1
clusterlab[p]=C

for (q in 2:nrow(NeighborPts))
    { testP=NeighborPts[q,1]

      if(lable[testP]==-2)
        lable[testP]=1

      newNeighborPts = regionQuery(testP, eps)



      if ((nrow(newNeighborPts)-1) >= MinPts)
          NeighborPts = rbind(NeighborPts,newNeighborPts)

      if(clusterlab[testP]==-3) #is not yet member of any cluster
      clusterlab[testP]=C
    }
return(hash)
}




regionQuery <- function(p, eps) {

  neighborhood=p
for(i in 1:nrow(x)){
      temp=sqrt((x[p,1]-y[i,1])^2+(x[p,2]-y[i,2])^2)
      if(temp<eps){
        c=c+1
        neighborhood=rbind(neighborhood,i)} 

        }
#neighborhood=neighborhood[-1,]
return(neighborhood)
}

当我打电话时

   haha=expandCluster(p, NeighborPts, C, eps, MinPts,hash,clusterlab,lable)

没用!!

我添加哈希变量来检查它。 每次调用 hash 的 expancdCLuster 都必须停止。但它并没有增加。

lable 和 clusterlab 也没有改变。

数据是 here

R 中的函数通常设计为按值而不是按引用传递参数。更新传入变量的值不会在调用环境中更改它们。一般来说,R 的方法是让你的函数 return 更新数据。如果你想 return 多个更新变量,你可以使用 list 来做到这一点。

您会看到人们使用分配给父环境运算符 (<<-),甚至 assign 到函数内的全局环境。这种编码风格可行,但它违背了函数通常不修改调用环境的原则,可能会使调试和将不同的代码片段集成到更大的项目中变得更加困难。