recode: Recode

Description Usage Arguments Author(s) Examples

Description

A simple function for recoding, which aims to replace nested ifelse statements. Typical use cases include; recoding numeric vectors into a factor, merging factor levels in an existing factor, and more.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
recode(x, ..., by = x, drop = TRUE, add = FALSE, as_factor = FALSE,
  arrange = FALSE)

recode_(x, dots, by = x, ...)

## Default S3 method:
recode_(x, dots, by, as_factor = FALSE, ...)

## S3 method for class 'factor'
recode_(x, dots, by, drop = TRUE, add = FALSE,
  arrange = FALSE, ...)

Arguments

x

A vector to be recoded.

...

Named arguments with the criteria for the recodes. In essence recode = criteria. . can also be used as a shorthand for the vector given in by.

by

The vector to recode values in x 'by'.

drop

Drop factor levels that have been recoded away from, and it has no remaining observations after the recode.

add

Set to TRUE to add levels to an existing factor variable.

as_factor

Convert numeric or character vectors to factor after recoding. Named recodes will be used as levels.

arrange

Set to TRUE to alphabetically sort the factor levels.

dots

A named list with either values that can be used with %in% or a boolean values.

Author(s)

Kristian D. Olsen

Examples

1
df %>% mutate(loy = recode(q15b, "Neg" = 1:6L, "Neu" = 7:8L, "Pos" = 9:10L, as_factor = TRUE))

itsdalmo/reporttool documentation built on May 18, 2019, 7:11 a.m.