Martund
Martund

Reputation: 301

How to create vectors in a loop

In R, when I write

i=10
y=c(i:i+9)
y

I get 19 in output, whereas when I directly write

y = c(10:19)
y

Why does this happen? What does the expression mean when written in terms of i? How to do that in a loop (because I have value stored in counter variable in that case)?

Upvotes: 0

Views: 68

Answers (1)

SmokeyShakers
SmokeyShakers

Reputation: 3412

Checkout:

?Syntax

: has higher precedence than +. It's an order of operations issue like arithmetic.

First:

10:10 

Evaluates to 10. Then:

10 + 9 

Evaluates to 19

Upvotes: 5

Related Questions