Huck Smith
Huck Smith

Reputation: 81

Does R have shorthand to generate a sequence of integers?

Does R have shorthand, function or operator which I can use to easily generate the following vector?

v1 <- c(1, 2, 3, 4, 5)

Something like

v1 <- 1..5

Upvotes: 0

Views: 1051

Answers (2)

NPE
NPE

Reputation: 500893

> 1:5
[1] 1 2 3 4 5

or

> seq(1, 5)
[1] 1 2 3 4 5

seq is quite flexible, in that it allows you to specify the stride, the desired number of output elements, etc., in various combinations:

## Default S3 method:
seq(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
    length.out = NULL, along.with = NULL, ...)

For example:

> seq(from=1, by=3, length.out=5)
[1]  1  4  7 10 13

Upvotes: 8

C. K. Young
C. K. Young

Reputation: 223183

Yes, you can use:

v1 <- 1:5

Upvotes: 2

Related Questions