在for循环中包含多个条件 [英] Including multiple conditions in for-loop
问题描述
我试图指定两个条件作为for循环的一部分。似乎在第二个循环的第二个条件没有被考虑。我的代码是这样运行的:
$ p $ for(i in 1:nrow(mydata)){
for(j in 1:nrow(mydata)&& j!= i){
语句...
}
语句...
}
请问如果这是R中正确的语法?
Thanks!
要回答您的问题,您需要:
$ b $ ($!$ b $ 1 $ n $($!$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ i){
语句...
}
}
语句...
}
然而,实现你想做的事情可能有一个更好的办法,但是我需要更多的细节。
I am trying to specify two conditions as a part of a for loop. It seems like the second condition in the second for loop is not being considered. My code runs this way:
for (i in 1:nrow(mydata)) {
for (j in 1:nrow(mydata) && j!=i ) {
Statements....
}
Statements...
}
Could you please tell me if this is the right Syntax in R? Thanks!
To answer your question, you need:
for (i in 1:nrow(mydata)) {
for (j in 1:nrow(mydata) ) {
if(j != i) {
Statements....
}
}
Statements...
}
However, there is probably a nicer way achieving what you want to do, but I would need more details.
这篇关于在for循环中包含多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!