recode | R Documentation |
Utility function that recodes variables based on user recode specifications. Handles both numeric or factor variables.
recode(var, recodes, afr, anr = TRUE, levels)
var |
Variable to recode |
recodes |
Character string of recode specifications:
|
afr |
Return a factor. Default is TRUE if |
anr |
Coerce result to numeric (default is TRUE) |
levels |
Order of the levels in the returned factor; the default is to use the sort order of the level names. |
Recoded variable
# Recode values from 1 to 9 to varios specifications var <- sample(x = 1:9, size = 100, replace = TRUE) # Recode single values recode(var = var, recodes = "9=NA") # Recode set of values recode(var = var, recodes = "c(1,2,5)=1") # Recode range of values recode(var = var, recodes = "1:3=1;4:6=2;7:9=3") # Recode other values recode(var = var, recodes = "c(1,2,5)=1;else=NA")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.