reorders {quest} | R Documentation |
Reorder Levels of Factor Data
Description
reorders
re-orders 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
.
Usage
reorders(data, fct.nm, ord.nm = NULL, fun, ..., suffix = "_r")
Arguments
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 re-order 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. |
Value
data.frame of re-ordered factor columns with colnames =
paste0(fct.nm, suffix)
.
See Also
Examples
# 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)