# reverse: Reverse the levels in a factor In kutils: Project Management Tools

## Description

Simple literal reversal. Will stop with an error message if x is not a factor (or ordered) variable.

## Usage

 `1` ```reverse(x, eol = c("Skip", "DNP")) ```

## Arguments

 `x` a factor variable `eol` values to be kept at the end of the list. Does not accept regular expresssions, just literal text strings representing values.

## Details

Sometimes people want to reverse some levels, excluding others and leaving them at the end of the list. The "eol" argument sets aside some levels and puts them at the end of the list of levels.

The use case for the `eol` argument is a factor with several missing value labels, as appears in SPSS. With up to 18 different missing codes, we want to leave them at the end. In the case for which this was designed, the researcher did not want to designate those values as missing before inspecting the pattern of observed values.

## Value

a new factor variable with reversed values

## Author(s)

Paul Johnson <pauljohn@ku.edu>

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36``` ```## Consider alphabetication of upper and lower x <- factor(c("a", "b", "c", "C", "a", "c")) levels(x) xr1 <- reverse(x) xr1 ## Keep "C" at end of list, after reverse others xr2 <- reverse(x, eol = "C") xr2 y <- ordered(x, levels = c("a", "b", "c", "C")) yr1 <- reverse(y) class(yr1) == "ordered" yr1 ## Hmm. end of list amounts to being "maximal". ## Unintended side-effect, but interesting. yr2 <- reverse(y, eol = "C") yr2 ## What about a period as a value (SAS missing) z <- factor(c("a", "b", "c", "b", "c", ".")) reverse(z) z <- factor(c(".", "a", "b", "c", "b", "c", ".")) reverse(z) ## How about R NA's z <- factor(c(".", "a", NA, "b", "c", "b", NA, "c", ".")) z reverse(z) z <- ordered(c(".", "a", NA, "b", "c", "b", NA, "c", ".")) z str(z) ## Put "." at end of list zr <- reverse(z, eol = ".") zr str(zr) z <- ordered(c(".", "c", NA, "e", "a", "c", NA, "e", "."), levels = c(".", "c", "e", "a")) reverse(z, eol = ".") reverse(z, eol = c("a", ".")) ```

kutils documentation built on April 30, 2020, 1:05 a.m.