Skip to content

Feature request: Order factor by (multiple functions of) multiple variables #16

@huftis

Description

@huftis

For plotting and tables, it’s useful to reorder levels of a factor according to other variables, first by one variable, and then by other variables to break any ties. The summary function used for each variable may be different. Example:

d = data.frame(
  name = factor(c("A", "A", "B", "B", "C", "C", "C", "D"),
                levels = LETTERS[4:1]),
  quality = c(5, 3, 4, 4, 7, 7, 7, 7),
  year = c(2000, 2001, 2013, 2014, 2015, 2015, 2015, 2015),
  weight = c(50, 45, 60, 57, 47, 50, 500, 63)
)

I want to order the name factor by 1) average quality, then 2) the first year the product appeared, and then 3) its median weight. If any ties remain, keep the original label order for these ties. In this example, the levels would be ordered ABCD.

To do this reordering, I have to think backwards, reordering by the last tie-breaker first, using either fct_reorder() or reorder():

d$name = fct_reorder(d$name, d$weight, median)
d$name = fct_reorder(d$name, d$year, min)
d$name = fct_reorder(d$name, d$quality, mean)

I would be very convenient to be able to do this in one go, using something like this (I’m dropping the d$ prefix to make the code clearer):

name = fct_reordern(name,
                    vars = list(quality, year, weight),
                    funs = list(mean, min, median))

It would be even better if the functions were shown along with the variable names, e.g. something like this (if possible, or perhaps using some form of formula syntax?):

name = fcr_reordern(name,
                    mean(quality), min(year), median(weight))

For descending order, perhaps by using a desc() function, like this?

name = fcr_reordern(name,
                    desc(mean(quality)), min(year), median(weight))

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions