Generating regular sequences

R has a number of facilities for generating commonly used sequences of numbers. For example 1:30 is the vector c(1, 2, …, 29, 30). The colon operator has high priority within an expression, so, for example 2*1:15 is the vector c(2, 4, …, 28, 30). Put n <- 10 and compare the sequences 1:n-1 and 1:(n-1).

The construction 30:1 may be used to generate a sequence backwards.

The function seq() is a more general facility for generating sequences.

seq(-5, 5, by=.2)
seq(length=51, from=-5, by=.2)

A related function is rep() which can be used for replicating an object in various complicated ways. The simplest form is

s5 <- rep(x, times=5)

which will put five copies of x end-to-end in s5. Another useful version is

s6 <- rep(x, each=5)

which repeats each element of x five times before moving on to the next.