For-Loop中的For-Loop似乎会跳过迭代(r) [英] For-Loop in For-Loop appears to skip iterations (r)
本文介绍了For-Loop中的For-Loop似乎会跳过迭代(r)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在for循环中运行for循环,但是它的行为不符合我的预期.我希望它会循环遍历内部"循环中的元素,然后交换到外部"循环中的下一个元素并重复.
I am trying to run a for loop in a for loop, however it doesn't behave in the way I expect. I would expect it to cycle through the elements in the 'inner' loop, and then swap to the next element in the 'outer' loop and repeat.
例如
Letters = c('AA', 'BB', 'CC', 'DD')
for(i in 1:length(Letters)) {
LetDup <- Letters
for(i in 1:length(LetDup)) {
Combined <- paste0(Letters[i], ' vs ', LetDup[i])
print(Combined)
}
}
我期望得到
AA vs AA
AA vs BB
AA vs CC
AA vs DD
BB vs AA
BB vs BB
.....
我该如何表明这就是我想要的,如果有人可以对我实际要求它做的事情提供简单的解释,那也将有所帮助.
How do I indicate that this is what I would like, and if someone could offer a simple explanation of what I've actually asked it to do, that would help as well.
推荐答案
如果您坚持使用循环,请引用@Dason的注释并执行以下操作:
If you insist on using loops, take @Dason's comment and do the following:
for(i in 1:length(Letters)) {
for(j in 1:length(Letters)) {
Combined <- paste0(Letters[i], ' vs ', Letters[j])
print(Combined)
}
}
这篇关于For-Loop中的For-Loop似乎会跳过迭代(r)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文