在for循环中包含多个条件 [英] Including multiple conditions in for-loop

查看:221
本文介绍了在for循环中包含多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图指定两个条件作为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆