Search code examples
rforcats

How to custom order levels of a factor variable by 2+ conditions?


   STP_name                                                           PCT
   <fct>                                                            <dbl>
 1 ENGLAND                                                           2.58
 2 MIDLANDS AND EAST OF ENGLAND                                      2.64
 3 LEICESTER, LEICESTERSHIRE AND RUTLAND STP                         2.31
 4 BATH, SWINDON AND WILTSHIRE STP                                   2.94
 5 KENT AND MEDWAY STP                                               2.8 
 6 BUCKINGHAMSHIRE, OXFORDSHIRE AND BERKSHIRE WEST STP               2.73
 7 NORTH CENTRAL LONDON STP                                          1.93
 8 NORTHAMPTONSHIRE STP                                              2.63
 9 SOMERSET STP                                                      3.41
10 SURREY HEARTLANDS STP                                             2.87

I would like to order the STP_name factor variable by their descending values in PCT, however I want the row for ENGLAND and MIDLANDS AND EAST OF ENGLAND to appear at the start of the factor levels regardless of their PCT value.

Ideally looking for a forcats solution but base is fine.


Solution

  • Assuming that the dataset have all unique 'STP_name', change it to factor with levels specified based on the order of 'PCT' and then add the two levels ("ENGLAND", ""MIDLANDS AND EAST OF ENGLAND") at the beginning with relevel or fct_relevel (from forcats)

    library(tidyverse)
    library(forcats)
    df1 %>%
        mutate(STP_name = fct_relevel(factor(STP_name, levels = STP_name[order(-PCT)]), 
                  c("ENGLAND",  "MIDLANDS AND EAST OF ENGLAND")))