在r中满足距离条件的样本N个单元
我是r编程的初学者,我尝试对25个单元格进行采样,所有单元格之间用最小距离“d”隔开。 我有一个在r中满足距离条件的样本N个单元,r,distance,cells,R,Distance,Cells,我是r编程的初学者,我尝试对25个单元格进行采样,所有单元格之间用最小距离“d”隔开。 我有一个 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){
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
我真的很感激能为这段代码提供一些建议
Alejandro您的问题有点不清楚,但如果我假设您使用的是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 ), ]
library(重塑2)
请提供一个样品。一个电池。从采样的单元格中删除过近的单元格。取样第二个电池。重复最后两个步骤25次。