Description Usage Arguments Value See Also Examples
View source: R/quest_functions.R
reorders
reorders the levels of factor data. The factors are columns
in a data.frame where the same reordering scheme is desired. This is often
useful before using factor data in a statistical analysis (e.g., lm
)
or a graph (e.g., ggplot
). It is essentially a vectorized version of
reorder.default
.
1 
data 
data.frame of data. 
fct.nm 
character vector of colnames in 
ord.nm 
character vector of length 1 or 
fun 
function that will be used to reorder the factor columns. The
function is expected to input an atomic vector of length =

... 
additional named arguments used by 
suffix 
character vector of length 1 specifying the string that will be appended to the end of the colnames in the return object. 
data.frame of reordered factor columns with colnames =
paste0(fct.nm, suffix)
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  # factor vector
reorder(x = state.region, X = state.region,
FUN = length) # least frequent to most frequent
reorder(x = state.region, X = state.region,
FUN = function(vec) {1 * length(vec)}) # most frequent to least frequent
# data.frame of factors
infert_fct < infert
fct_nm < c("education","parity","induced","case","spontaneous")
infert_fct[fct_nm] < lapply(X = infert[fct_nm], FUN = as.factor)
x < reorders(data = infert_fct, fct.nm = fct_nm,
fun = length) # least frequent to most frequent
lapply(X = x, FUN = levels)
y < reorders(data = infert_fct, fct.nm = fct_nm,
fun = function(vec) {1 * length(vec)}) # most frequent to least frequent
lapply(X = y, FUN = levels)
# ord.nm specified as a different column in data.frame
z < reorders(data = infert_fct, fct.nm = fct_nm, ord.nm = "pooled.stratum",
fun = mean) # category with highest mean for pooled.stratum to
# category with lowest mean for pooled.stratum
lapply(X = z, FUN = levels)

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.