【问题标题】:Skip only first line in the first iteration在第一次迭代中只跳过第一行
【发布时间】:2021-12-26 07:04:03
【问题描述】:

我试图在循环内的第一次迭代中仅跳过一个命令或行,然后继续工作:

我成功地做到了,但我想知道是否有某种方法可以更简单地完成这项任务。

我只想在第一次迭代中省略第一行,我有多行:

      for(n in 1:5) {
               if(n == 1)   {
                    # I wanna omit this line below, my real script not have the line below
                    #print(paste0("This is my ",n, " iteration, continue running my script"))
           
                    print(n)
                    print("I only wanna omit the first line  in the beginning of loop, then continue my work")

                    }else{

                    print(paste0("This is my ",n, " iteration, continue running my script"))
                    print("I only wanna omit the first line  in the beginning of loop, then continue my work")
                    }
             }

             [1] 1
             [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
             [1] "This is my 2 iteration, continue running my script"
             [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
             [1] "This is my 3 iteration, continue running my script"
             [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
             [1] "This is my 4 iteration, continue running my script"
             [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
             [1] "This is my 5 iteration, continue running my script"
             [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"

我试过了:

    for(n in 1:5) {

            if(n==1)  
            print(paste0("I only wanna omit the first line  in the beginning of loop, then continue my work")) 

            print(paste0("This is my ",n, " iteration, continue running my script"))  ;next

    } 

但不工作:

    [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
    [1] "This is my 1 iteration, continue running my script"
    [1] "This is my 2 iteration, continue running my script"
    [1] "This is my 3 iteration, continue running my script"
    [1] "This is my 4 iteration, continue running my script"
    [1] "This is my 5 iteration, continue running my script"

再试一次:

    for(n in 1:5) {

          if(n==1)  
          print(paste0("I only wanna omit the first line  in the beginning of loop, then continue my work")) ;next
          print(paste0("This is my ",n, " iteration, continue running my script"))  

    } 

  [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"

尝试 3:

    for(n in 1:5) {
           if(n == 1)   {
                print("I wanna skip this line in first iteration")
                print(n)
                print("I only wanna omit the first line  in the beginning of loop, then continue my work")
               
                print("I dont wanna skip this line in first iteration")
                print(n)
                print(paste0("This is my ",n, " iteration, continue running my script"))
               }  ;next
         }

             [1] "I wanna skip this line in first iteration"
             [1] 1
             [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
             [1] "I dont wanna skip this line in first iteration"
             [1] 1
             [1] "This is my 1 iteration, continue running my script"

【问题讨论】:

  • 为什么不只是for (n in 2:5) …? ——也就是说,你在这里迭代什么?可能有更好的解决方案,但这完全取决于您的循环实际在做什么。
  • @Adam 是的,我想要这个
  • @Konrad Rudolph 如果我使用:for (n in 2:5),我会省略所有第一次迭代,但我只想在第一次迭代中省略第一行。
  • 认为它不是跳过第一个 if (n == 1) 而是运行第一个 if (n > 1)。应该少一点逻辑。 if (n > 1) {... stuff} 然后您可以正常使用其余代码,而不必在 else 语句中。
  • @adam,你是对的。这对我有用。谢谢

标签: r loops


【解决方案1】:

感谢@Adam 评论,因为这帮助我得到正确答案,比我想象的更容易:

        for(n in 1:5) {
              if(n > 1)
              print("I wanna skip this line in first iteration")  
                 print(n)
                 print("I only wanna omit the first line  in the beginning of loop, then continue my work")
        
                 print("I dont wanna skip this line in first iteration")
                 print(n)
                 print(paste0("This is my ",n, " iteration, continue running my script")) 
       
  } 

  [1] 1
  [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
  [1] "I dont wanna skip this line in first iteration"
  [1] 1
  [1] "This is my 1 iteration, continue running my script"
  [1] "I wanna skip this line in first iteration"
  [1] 2
  [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
  [1] "I dont wanna skip this line in first iteration"
  [1] 2
  [1] "This is my 2 iteration, continue running my script"
  [1] "I wanna skip this line in first iteration"
  [1] 3
  [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
  [1] "I dont wanna skip this line in first iteration"
  [1] 3
  [1] "This is my 3 iteration, continue running my script"
  [1] "I wanna skip this line in first iteration"
  [1] 4
  [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
  [1] "I dont wanna skip this line in first iteration"
  [1] 4
  [1] "This is my 4 iteration, continue running my script"
  [1] "I wanna skip this line in first iteration"
  [1] 5
  [1] "I only wanna omit the first line  in the beginning of loop, then continue my work"
  [1] "I dont wanna skip this line in first iteration"
  [1] 5
  [1] "This is my 5 iteration, continue running my script"

【讨论】:

    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    相关资源
    最近更新 更多