recodes: Recode Data

Description Usage Arguments Value See Also Examples

View source: R/quest_functions.R


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.frame of data.


character vector of colnames from data specifying the variables.


character vector of length 1 specifying the recodes. See details of recode for how to use this argument.


character vector of length 1 specifying the string to add to the end of the colnames in the return object.


logical vector of length 1 specifying if the recoded columns should be returned as factors. The default depends on the column in data[vrb.nm]. If the column is a factor, then as.factor = TRUE for that column. If the column is not a factor, then as.factor = FALSE for that column. Any non-default, specified value for this argument will result in as.factor being universally applied to all columns in data[vrb.nm].


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[vrb.nm] which are typeof character and have letters in their values (e.g., "1a"). Note, this argument is often not needed as you can directly recode to a numeric by excluding quotes from the number in the recodes argument.


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.

See Also

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

quest documentation built on Sept. 10, 2021, 5:07 p.m.

Related to recodes in quest...