# 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)
Run the code above in your browser using DataLab