R: Shift factor levels to left or right, wrapping around at end

fct_shift {forcats} | R Documentation

## Shift factor levels to left or right, wrapping around at end

### Description

This is useful when the levels of an ordered factor are actually cyclical, with different conventions on the starting point.

### Usage

    fct_shift(f, n = 1L)

### Arguments

`f` | A factor.
`n` | Positive values shift to the left; negative values shift to the right.

### Examples

    x <- factor(
      c("Mon", "Tue", "Wed"),
      levels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),
      ordered = TRUE
    )
    x
    fct_shift(x)
    fct_shift(x, 2)
    fct_shift(x, -1)

[Package forcats version 0.3.0 Index]