R中带有while循环的函数中的min()错误-R爱好者面临的调试挑战

R中带有while循环的函数中的min()错误-R爱好者面临的调试挑战,r,debugging,while-loop,min,R,Debugging,While Loop,Min,我有一个函数(bobB)似乎陷入了一个while循环。当我点击escape并查看warnings()时,我得到以下错误: Warning message: In min(xf, yf) : no non-missing arguments to min; returning Inf 一些示例代码: #I have the data: x<-"A03" y<-"A24" sitex<-c("Sp1","Sp1","Sp3","Sp3") sitey<-c("S

我有一个函数(bobB)似乎陷入了一个while循环。当我点击escape并查看warnings()时,我得到以下错误:

Warning message:
In min(xf, yf) : no non-missing arguments to min; returning Inf  
一些示例代码:

#I have the data: 

x<-"A03" 
y<-"A24"

sitex<-c("Sp1","Sp1","Sp3","Sp3")
sitey<-c("Sp2","Sp4","Sp2","Sp4")
gsim<-c(0.2,0.3,0.4,0.1)
gsim<-data.frame(sitex,sitey,gsim)

site<-c("A03","A03","A03","A03","A24","A24","A24","A24")
species<-c("Sp1","Sp1","Sp3","Sp4","Sp1","Sp1","Sp3","Sp4")
freq<-c(0.2,0.3,0.4,0.1,0.3,0.3,0,0.4)
ssf<-data.frame(site,species,freq,stringsAsFactors=FALSE)

#My function:  

bobB <- function (x, y, ssf, gsim) {

#*Step 1.* Create an empty matrix 'specfreq' to fill 

#Selects the species frequency data greater than 0 for the two sites being compared  

ssfx <- ssf[ssf$site == x & ssf$freq >0,]
ssfy <- ssf[ssf$site == y & ssf$freq >0,]

#pull out the species that are present at site x and/or site y using a merge,  
#this is    needed to create the initial empty matrix  

m<-(merge(ssfx,ssfy,all=TRUE))
species<-unique(m$species)

#Creates an empty matrix of the frequency of each species at site x and y  

 specfreq <- matrix(0, length(species), 2, dimnames=list(species,c(x,y)))

#*Step 2.* Fill empty matrix 'specfreq' with data from data.frame 'ssf'  

for(i in 1:nrow(ssf{specfreq[rownames(specfreq)==ssf[i,"species"],colnames(specfreq)==ssf[i,"site"]] <- ssf[i,"freq"]}

#*Step 3.* For species present at site x and y remove the minimum of the two from both  
#find minimum frequency for each species for site x and y  

a <- pmin(specfreq[,x], specfreq[,y])

#subtract 'a' from current 'specfreq'  

specfreq <- specfreq - a

#*Step 4.* Calulate variable B  

#Set answer to 0

answer <- 0

#while 'specfreq' contains data (i.e. is >0) keep doing the following  

while(sum(specfreq) > 1e-10) {

#Find species remaining at site x  

sx <- species[specfreq[,1] > 0]

#Find species remaining at site y  

sy <- species[specfreq[,2] > 0]     

#Pull out the gsim value for sx and sy

gsimre <-gsim[gsim$sitex %in% sx & gsim$sitey %in% sy,]

#determines the row containing the maximum value for remaining gsim and assigns it to i     

i <- which.max(gsimre$gsim)

#once the max gsim value has been found (i) we go back to the specfreq matrix and filter  
#out the frequency of the site x species associated with i   

xf <- specfreq[(gsimre$sitex[i]),x]

#and the frequency of the site y species associated with i  

yf <- specfreq[(gsimre$sitey[i]),y]

#The frequency of the species at site x associated with the greatest i is multiplied by i           

answer <- answer + xf * gsimre$gsim[i]

#Then take the minimum common frequency for the two species associated with i  

f <- min(xf,yf)

#Subtract theminimum  common frequency from both the site x and site y column  

specfreq[gsimre$sitex[i],x] <- specfreq[gsimre$sitex[i],x]-f
specfreq[gsimre$sitey[i],y] <- specfreq[gsimre$sitey[i],y]-f   
}
answer
}
bobB(x, y, ssf, gsim)
#我有以下数据:

好的,这就是你的问题(我认为):

Sp4是第二个因子,其数值为2,因此
specfreq[(gsimre$sitey[i]),y]
specfreq[2,2]
,即0

因此,这应该可以做到:

xf <- specfreq[as.character(gsimre$sitex[i]),x]
yf <- specfreq[as.character(gsimre$sitey[i]),y]
由于这个错误,
yf
等于0,因此
f=min(xf,yf)
始终等于0,因此您的循环是无止境的

注意:
answer
应该在
while
循环之外,如果您希望函数返回它,即

    }
answer
}
而不是

    answer
    }
}

调试建议:如果您能让我更容易地将代码复制并粘贴到干净的会话中,我可能会看看这个。因此,对于初学者来说,将您的注释更改为嵌入式代码注释,或者将其删除(即,给我一个可以工作的代码块)。为什么您忽略了您之前发布的这个问题的注释和答案?很明显,
xf
yf
是空的,所以您需要学习调试。向后查看
x,y,gsimre$sitex[i]
等的值,直到找到错误为止。@Andrie谢谢。我已将问题重新格式化为一段代码,如下所示suggested@Carl维特霍夫特:我并没有忽视对我关于min函数的问题所作的评论。我只是采纳了一些评论的建议,并完整地陈述了问题。我还不清楚问题出在哪里。谢谢plannapus。这似乎奏效了,我现在可以取笑巴布了。然而,当我运行“外部函数”时,它结合了函数bobA、bobB和bobC,我仍然会得到错误“In min(xf,yf):min没有未丢失的参数;返回Inf”。不过,我将把这作为一个不同的问题发布。再次感谢!
gsim<-data.frame(sitex,sitey,gsim, stringsAsFactors=FALSE)
    }
answer
}
    answer
    }
}