View source: R/quest_functions.R
recodes | R Documentation |
recodes
recodes data based on specified recodes using the
car::recode
function. This can be used for numeric or character
(including factors) data. See recode
for details. The
levels
argument from car::recode
is excluded because there is
no easy way to vectorize it when only a subset of the variables are factors.
recodes(data, vrb.nm, recodes, suffix = "_r", as.factor, as.numeric = TRUE)
data |
data.frame of data. |
vrb.nm |
character vector of colnames from |
recodes |
character vector of length 1 specifying the recodes. See
details of |
suffix |
character vector of length 1 specifying the string to add to the end of the colnames in the return object. |
as.factor |
logical vector of length 1 specifying if the recoded columns
should be returned as factors. The default depends on the column in
|
as.numeric |
logical vector of length 1 specifying if the recoded
columns should be returned as numeric vectors when possible. This can be
useful when having character vectors converted to numeric, such that
numbers with typeof character (e.g., "1") will be coerced to typeof numeric
(e.g., 1). Note, this argument has no effect on columns in
|
data.frame of recoded variables with colnames specified by
paste0(vrb.nm, suffix)
. In general, the columns of the data.frame
are the same typeof as those in data
except for instances when
as.factor
and/or as.numeric
change the typeof.
recode
reverses
recodes(data = psych::bfi, vrb.nm = c("A1","C4","C5","E1","E2","O2","O5"),
recodes = "1=6; 2=5; 3=4; 4=3; 5=2; 6=1")
re_codes <- "'Quebec' = 'canada'; 'Mississippi' = 'usa'; 'nonchilled' = 'no'; 'chilled' = 'yes'"
recodes(data = CO2, vrb.nm = c("Type","Treatment"), recodes = re_codes,
as.factor = FALSE) # convert from factors to characters
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.