seq
Generate a Sequence

Description

Creates a vector of evenly-spaced numbers. You can specify the start, end, spacing, and length of the sequence.

Usage

seq(...)
seq.default(from = 1, to = 1, by = ((to - from)/(length.out - 1)),
    length.out = NULL, along.with = NULL, ...)
seq.int(from, to, by, length.out, along.with, ...)
seq_along(along.with)
seq_len(length.out)
from:to

Arguments

from starting value of the sequence. This argument is required by : (the colon operator). If to, by, and length.out are all given, the value for from is inferred. Otherwise, the default is 1.
to ending value of the sequence. This argument is required by : (the colon operator). If from, by, and length.out are all given, the value for to is inferred. Otherwise, the default is 1. A value of to that is less than from is allowed.
by spacing between successive values in the sequence. If from, to, and length.out are all given, the value for by is inferred. Otherwise, the default is 1.
length.out number of values in the sequence. If from, to, and by are all given, the value for length.out is inferred.
along.with an object. The length of the object is used as the length of the sequence.
... other arguments passed to or from methods.

Details

If seq is called with one unnamed numeric argument of length 1, it returns an integer sequence from 1 to the value of the argument. For example, the command seq(4) returns the integers 1, 2, 3, 4.
To generate an integer sequence from 1 to length(x) for an object x, use seq(along.with=x) or seq(length.out=length(x)) or the faster convenience functions seq_along(x) or seq_len(length(x)). If x has length 0 or 1, seq(along.with=x) produces a sequence of length 0 or 1, and seq(x) produces the integer sequence from 1 to x.
The from value can be larger or smaller than the to value. If by is specified, it must have the appropriate sign for a finite sequence to be generated. That is, by must be negative if from is larger than to. If the difference between from and to is not a multiple of by, the sequence stops at the last value that is not past to.
seq_along produces the vector of indices of a given vector.
seq_len produces the sequence of integer numbers from 1 to length.out. If the argument is other than numeric, (for example, vector or matrix), seq_len generates the sequence of integers from 1 to the first argument of the given object.
seq.default generates a sequence of numbers.
Value
returns a numeric vector with values (from, from+by, from+2*by, ..., to).
Note
The colon operator (:) has a high precedence. (See the help file for Syntax.) Therefore, parentheses are needed to create a sequence from 1 to n-1. For example, use the command 1:(n-1) instead of 1:n-1. (If you omit the parentheses, the engine subtracts 1 from each of the values in the sequence 1:n.)
If by is very small and to is specified, the final element in the returned sequence might not equal to (although it will be extremely close). This phenomenon is due to finite precision arithmetic. You can work around this issue by using a different choice of operators. For example, the sequence produced by seq(0,1,by=1/n) might not end exactly at 1 if n is very large. Instead, you can use the commands seq(0,1,length=n+1) or 0:n/n to create a sequence that ends in 1.
As a special case, if from and to are both factors then from:to returns the same as interaction(from, to, sep=":", lex.order=TRUE).
See Also
seq.dates, rep, c, seq.POSIXt, seq.Date, sequence, interaction.
Examples
# Three ways to obtain the sequence 1, 2, 3, 4, 5.
seq(5)
1:5
seq_len(5)
# The sequence 5, 4, 3, 2, 1.
5:1
# Integer sequence from 1 to -5.
seq(-5)
# The sequence 1.1, 2.1, 3.1, 4.1.
1.1 : 5
# The sequence 0, 0.01, 0.02, ..., 1.
seq(0, 1, by=0.01)
# 100 values from -pi to pi.
seq(-pi, pi, length=100)
# complex sequence
seq(4+5i, 32+6i, length = 5)

seq(2, 2, 0) # 2

# Examples for seq.int seq.int(6, 2, -1) # [1] 6 5 4 3 2

seq.int(along.with=8) # [1] 1

seq.int(length.out=8) # [1] 1 2 3 4 5 6 7 8

# Examples for seq_along x <- c(NA, 1, 3, 5, 7, 9, NA, Inf) seq_along(x) # [1] 1 2 3 4 5 6 7 8

mat <- matrix(c(6, 3, 1, 1, 2, 5), ncol=2) seq_along(mat) # [1] 1 2 3 4 5 6

# Examples for seq_len seq_len(0) # interger(0)

Package base version 6.0.0-69
Package Index