Hmisc (version 2.0-9)

reorder.factor: Reorder Factor Levels

Description

Reorders the levels of a factor variable by the values or the summarized values of another variable

Usage

reorder.factor(x, v, FUN = mean, ...)

Arguments

x
a factor variable
v
a numeric variable the same length as x
FUN
a statistical summarization function applied to v by levels of x
...
other arguments passed to FUN

Value

  • a new factor vector

See Also

factor

Examples

Run this code
x <- factor(c('a','b','b','c'))
v <- c(3,-1,1,-5)
w <- reorder.factor(x, v)  # uses FUN=mean
w
levels(w)
class(w)

Run the code above in your browser using DataCamp Workspace