0
numbers = 1:100

for(x in seq(26,1,-2)) {
  print(numbers[x:x+2]) 
}

And the above prints:

[1] 28
[1] 26
    {etc}
[1] 4

My question is why doesn't the loop print something like a section (26 27 28) of the list? Why does it only print out one number per iteration? And what would I have to change for the output to be slices of the list? Desired out put

[1] 27 28
[1] 25 26
   {etc}
[1] 3 4

This is confusing to me since

numbers[1:5]
# prints [1] 1 2 3 4 5
Rafael
  • 3,096
  • 1
  • 23
  • 61
  • 1
    Use `numbers[x:(x+2)]`. Check out the `?Syntax` help page for the order of operations of different operators. – MrFlick Apr 06 '17 at 15:55

1 Answers1

2

You need some parentheses, otherwise it is (x:x)+2

for(x in seq(26,1,-2)) {
  print(numbers[x:(x+2)]) 
}
Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32