Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reordering rows alphabetically with specific exception(s) in R

This is my first Stack Overflow question so bear with me please. I’m trying to create dataframes that are ordered alphabetically based on a "Variable" field, with exceptions made for rows of particular values (e.g. "Avg. Temp" at the top of the dataframe and "Intercept" at the bottom of the dataframe). The starting dataframe might look like this, for example:

              Variable       Model 1 Estimate


               Year=2009         0.026
               Year=2010        -0.04
               Year=2011        -0.135***
                  Age            0.033***
               Avg Temp.        -0.001***
               Intercept        -3.772***
                  Sex           -0.073***
               Year=2008         0.084***
               Year=2012        -0.237***
               Year=2013        -0.326***
               Year=2014        -0.431***
               Year=2015        -0.589***

And I want to reorder it as such:

              Variable       Model 1 Estimate


               Avg Temp.        -0.001***
                  Age            0.033***
                  Sex           -0.073***
               Year=2008         0.084***
               Year=2009         0.026
               Year=2010        -0.04
               Year=2011        -0.135***
               Year=2012        -0.237***
               Year=2013        -0.326***
               Year=2014        -0.431***
               Year=2015        -0.589***
               Intercept        -3.772***

Appreciate any help on this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the fct_relevel() function from {forcats}. Its first call put Avg Temp., Age and Sex at the beginning (after = 0 by default). The second call will put Intercept at the end (n() refers to the numbers of line in the data frame).

library(tidyverse)

df <-
  tribble(~Variable, ~Model,
          "Year=2009", 0.026,
          "Year=2010", -0.04,
          "Year=2011", -0.135,
          "Age", 0.033,
          "Avg Temp.", -0.001,
          "Intercept", -3.772,
          "Sex", -0.073,
          "Year=2008", 0.084,
          "Year=2012", -0.237,
          "Year=2013", -0.326,
          "Year=2014", -0.431,
          "Year=2015", -0.589)

df %>% 
  mutate(Variable = as.factor(Variable),
         Variable = fct_relevel(Variable, "Avg Temp.", "Age", "Sex"),
         Variable = fct_relevel(Variable, "Intercept", after = n())) %>% 
  arrange(Variable)

# A tibble: 12 × 2
   Variable   Model
   <fct>      <dbl>
 1 Avg Temp. -0.001
 2 Age        0.033
 3 Sex       -0.073
 4 Year=2008  0.084
 5 Year=2009  0.026
 6 Year=2010 -0.04 
 7 Year=2011 -0.135
 8 Year=2012 -0.237
 9 Year=2013 -0.326
10 Year=2014 -0.431
11 Year=2015 -0.589
12 Intercept -3.77 


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading