Learn R Programming

SciencesPo (version 1.3.5)

shift: Shifts elements of a vector left or right by N positions (Lag or Lead).

Description

Shifts elements of a vector left or right by N positions (Lag or Lead).

Usage

shift(x, ...)

## S3 method for class 'default': shift(x, n = 1, pad = TRUE, wrap = TRUE, ...)

## S3 method for class 'data.frame': shift(x, ...)

Arguments

x
A vector to be operated on
n
Number of rows to shift by (if negative, shift to right instead of left)
pad
Whether to pad with NAs or not. pad does nothing unless wrap is false, in which case it specifies whether to pad with NAs
wrap
Whether to wrap elements or not (adds the entry at the beginning to the end)
...
Other items to pass along

Value

  • vector of the same type as vec

encoding

UTF-8

Examples

Run this code
l <- list(a = sample(LETTERS,3), b = runif(5), c = runif(15));
df <- as.data.frame(l);
shift(df$c, 1)

Run the code above in your browser using DataLab