Data Sequences and Repetition in R

Data sequences and repetition are useful functions to define data objects, create new objects, control extractions or replacement, and manage function routines.

Data Sequences

The seq() function can be used several ways depending on its argument structure:

The first form generates the sequence from a number to a number and is identical to from:to:

The second form generates a sequence from:to with the step length by:

The third generates a sequence of length.out equally spaced values from from to to. (length.out is usually abbreviated to length or len, and seq_len is much faster):

The fourth form generates the sequence 1, 2, ..., length(along.with). (along.with is usually abbreviated to along, and seq_along is much faster):

The fifth form generates the sequence 1, 2, ..., length(from) (as if argument along.with had been specified), unless the argument is numeric of length 1 when it is interpreted as 1:from:

The final form generates 1, 2, ..., length.out unless length.out = 0, when it generates integer(0).

The seq() function is also very useful for creating date/time series:

Data Repetitions

The rep() function will replicate the value of x, depending on the arguments used:

Back | Next

Leave a Reply

Your email address will not be published. Required fields are marked *