View source: R/S03_Utilities.R
recode | R Documentation |
Function to flexibly recode values of a variable. Can match values and replace with new codes, assign codes based on intervals for a continuous variable, or code a linear trend given a start and end point.
recode(x, values, codes = NULL, default = 0, type = NULL)
x |
A vector. |
values |
Either a vector of values to
replace, or a list of vectors for the sets of values
to replace. Values of the form |
codes |
A vector of values, either a single
value or a vector matching in length with |
default |
The default value to return as output for all cases that are not recoded. |
type |
A character string, either |
A vector.
# Assigning by matching categories
x <- rep( LETTERS[1:3], each = 3 )
recode( x, list( 'A', c('B', 'C') ) )
recode( x, c( 'A', 'B', 'C' ), c( -1, 0, 1 ) )
# Assigning by intervals
x <- seq( -1.5, 1.5, .5 )
cbind(x, recode( x, c( '(-Inf,0]', '(0,Inf]' ) ) )
# Linear trend
cbind(x, recode( x, '-1,1' ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.