r-下标越界,在最后一个值处停止循环



我一直收到错误:

Error in locoh[[i + 1]] : subscript out of bounds

我几乎可以肯定,这是因为我在寻找额外的值,所以length(locoh) +1,但那个值不存在。如何在对象locoh的最大范围内停止此for循环?

# Estimate overlap between LoCoHs for each 10-day interval
for (i in 1:(length(locoh))){
hr_overlap(locoh[[i]], locoh[[i+1]], type = 'hr')
}

例如,我希望For循环从locoh, then locoh+1, locoh + 2, .....中的第一个值开始,然后在到达locoh_max value时停止。最好的方法是什么?

与其循环到length,不如循环到length(locoh)-1,因为locoh[[i+1]]位于最后一个i,即length将超出的范围

for (i in 1:(length(locoh)-1)){
hr_overlap(locoh[[i]], locoh[[i+1]], type = 'hr')
}

最新更新