Sequences in R
Generating sequences in R is another easy way to get a series of numbers. Vectors - which we already covered in another article - can create a series of numbers. However, they're very simple sequences and only increment by one. If we need a series that increments by 0.5, 10, or even 50 we need to use the seq() function. Unlike a simple vector, with sequences we can specify the interval. Let's see numbers one through three, incrementing by 0.25:
> seq(to=1, from=3, by=0.25)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00
> seq(1, 3, 0.25)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00
Something really handy about seq() is that we can feed it to and from inputs, then specify length. The length option tells seq() how many values we want to see between to and from values. Let's use seq() to create a sequence of nine numbers between one and three:
> seq(1, 3, length=9)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00
Sequences are a great alternative to vectors we we need more fine-grained control over the increment.