r - Complex/Mixed sort column in data frame -


i have column in dataframe this...

retention_completion_variable_name <- data.frame(   retention_completion_variable_name = c(     "completed degree in 1 year",     "retained midyear year 1",     "completed degree in 2 years",     "retained midyear year 2",     "retained start of year 2"   ),   retention_completion_value = c(0, 0, 0, 1, 1),   stringsasfactors = false )    

i want sort column

       retained midyear year 1                     0                      retained start of year 2                    1                retained midyear year 2                     1                     completed degree in 1 year                     0                    completed degree in 2 years                    0               

this 1 of few cases feel factor() useful:

lvls <- c("retained midyear year 1", "retained start of year 2",            "retained midyear year 2", "completed degree in 1 year",            "completed degree in 2 years") dt$retention_completion_variable_name <-    factor(dt$retention_completion_variable_name, levels = lvls) dt <- dt[order(dt$retention_completion_variable_name), ] dt 
  retention_completion_variable_name retention_completion_value 2         retained midyear year 1                          0 5        retained start of year 2                          1 4         retained midyear year 2                          1 1         completed degree in 1 year                          0 3        completed degree in 2 years                          0 

data

dt <- as.data.frame(readr::read_table(   "retention_completion_variable_name      retention_completion_value         completed degree in 1 year                         0                retained midyear year 1                         0                  completed degree in 2 years                        0                  retained midyear year 2                         1                 retained start of year 2                        1    " )) 

enhancement

in case there many years cover, creation of factor levels hand quite cumbersome , error-prone. however, can automated observing 3 rules

  1. all "retained" come before "completed".
  2. within retained it's ordered year , within year "start" , "midyear".
  3. within "completed" it's ordered year.

these rules can used create factor levels programmatically:

n_years <- 5l lvls <- c(paste(c("retained start of year", "retained midyear year"),                  rep(seq_len(n_years), each = 2l)),           sprintf("completed degree in %i years", seq_len(n_years))) lvls 
 [1] "retained start of year 1" "retained midyear year 1"  "retained start of year 2"  [4] "retained midyear year 2"  "retained start of year 3" "retained midyear year 3"   [7] "retained start of year 4" "retained midyear year 4"  "retained start of year 5" [10] "retained midyear year 5"  "completed degree in 1 years" "completed degree in 2 years" [13] "completed degree in 3 years" "completed degree in 4 years" "completed degree in 5 years" 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -