2013-08-05 32 views
-2

我是一個r編程的初學者,我試圖對25個細胞進行採樣,它們之間用最小距離「d」分隔。 我有一個在r編程中滿足距離條件的樣本N個細胞

b=c()       # vector to store the selected cells 
b[1]=sample(a,1)    # randomly sampling the first cell from my landscape  
x=c() 
for (i in 1:24){      
    b2=sample(a,1)    #propose a candidate cell 
    for(j in 1:i){    
    if (DIJ[b[j],b2]>=d){ #check if the distance between the proposed and ALL the   
    x[i]=1      chosen 
          #cells meets the distance, where DIJ is a distance matrix 
}       assign 1 if it does, 0 otherwise 
    else{x[i]=0    
}} 
    while(sum(x)==i){  #If the sum equals i, then the proposed meets the requirement 
    b2=b[i+1]   against all the cells, otherwise start again with a new  
    }}     proposal 

我會很感激的建議,使這片代碼工作。

亞歷

+1

請提供[再現的示例](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。請提供 – Thomas

+0

樣品數據! – Metrics

+0

採樣一個單元。刪除距採樣單元太近的單元。樣品第二個單元。重複最後兩步25次。 –

回答

1

你的問題是有點不清楚,但如果我假設你有一個距離矩陣工作由DIST()創建的,那麼這將做到這一點。有可能是一種更有效的方法。

library(reshape2) 
m <- matrix(rnorm(100), nrow = 25) 
d <- dist(m, upper=T, diag=T) 

l <- melt(as.matrix(d)) 

# sample 10 values, where value > 1 
l[ sample(which(l$value > 1), 10), ]