简体   繁体   中英

R For Loop - Increment by 5's but start at 1

I would think this has come up before, but cant seem to find the answer.

I want to loop from 1:20, by 5. I can do this using:

for (i in seq(1,20,5)){

print(i)
}

But I would like for my sequencing to go: 1,5,10,15,20 . I know that I can do some manual stuff like the following, but this is way too cumbersome to do repeatedly. Is there some easily built in way of doing this in R?

for (i in seq(1,21,5)){

  if (i != 1){
     i <- i-1
  }

  print(i)

}

一种选择是将1与seq输出串联

c(1, seq(5, 20, by = 5))

Try

c(1,seq(5,20,5))

Because the difference from 1 to 5 is different from the other steplength, adding 1 maunally is probably best.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM