1

Fairly simple question that i'm struggling a bit with.

I want to create a for loop function that goes through 'i in 1:500'. However, I want it to specifically exclude a small list of numbers (i.e 67, 106, 188).

Just trying to work out the most efficient way of doing this.

Thanks

Ben G
  • 95
  • 9

2 Answers2

3

You can simply subset the 1:500 like this:

exclude <- c(67, 106, 188)
for(i in (1:500)[-exclude]){
...
}

or

for(i in seq(500)[-exclude]){
...
}
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

You could also build a vector of the i's you want and the run it through seq_along: Kind of the opposite version of Allan's answer.

x <- 1:50
y <- c(1:25)
for(i in seq_along(y)){
  print(x[[i]])
}
H.Stevens
  • 391
  • 3
  • 16