2013-08-05 29 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), ]