0

while ループでスタックしているように見える関数 (bobB) があります。エスケープを押して 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)
4

1 に答える 1

0

わかりましたので、ここにあなたの問題があります(私は思います):

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

(gsimre$sitex[i])および(gsimre$sitey[i])は、それらを使用してインデックスを作成すると、文字ではなく数値として解釈されることを意味する要因です。本当に:

gsimre$sitey
[1] Sp4
Levels: Sp2 Sp4  

2 番目の要素である 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]

ssfまたは、最初にそれらを文字列であり、要素ではないとして宣言しますgsim

gsim<-data.frame(sitex,sitey,gsim, stringsAsFactors=FALSE)

この間違いのためyfに 0 に等しかったため、f=min(xf,yf)常に 0 に等しかったため、無限ループが発生しました。

PS:関数がそれを返すようにしたい場合answerは、ループの外にある必要があります。whilee.

    }
answer
}

それ以外の

    answer
    }
}
于 2012-06-21T11:42:48.533 に答える