mixedsort               package:gtools               R Documentation

_O_r_d_e_r _o_r _S_o_r_t _s_t_r_i_n_g_s _w_i_t_h _e_m_b_e_d_d_e_d _n_u_m_b_e_r_s _s_o _t_h_a_t _t_h_e _n_u_m_b_e_r_s
_a_r_e _i_n _t_h_e _c_o_r_r_e_c_t _o_r_d_e_r

_D_e_s_c_r_i_p_t_i_o_n:

     These functions sort or order character strings containing numbers
     so that the numbers are numerically sorted rather than sorted by
     character value.  I.e. "Asprin 50mg" will come before "Asprin
     100mg". In addition, case of character strings is ignored so that
     "a", will come before "B" and "C".

_U_s_a_g_e:

     mixedsort(x)

_A_r_g_u_m_e_n_t_s:

       x: Character vector to be sorted 

_D_e_t_a_i_l_s:

     I often have character vectors (e.g. factor labels) that contain
     both text and numeric data, such as compound and dose.  This
     function is useful for sorting these character vectors into a
     logical order.

     It does so by splitting each character vector into a sequence of
     character and numeric sections, and then sorting along these
     sections, with numbers being sorted by numeric value (e.g. "50"
     comes before "100"), followed by characters strings sorted by
     character value (e.g. "A" comes before "B").

     Empty strings are always sorted to the front of the list, and 'NA'
     values to the end.

_V_a_l_u_e:

     'mixedorder' returns a vector giving the sort order of the input
     elements. 'mixedsort' returns the sorted vector.

_A_u_t_h_o_r(_s):

     Gregory R. Warnes gregory.r.warnes@pfizer.com

_S_e_e _A_l_s_o:

     'sort', 'order'

_E_x_a_m_p_l_e_s:

     # compound & dose labels
     Treatment <- c("Control", "Asprin 10mg/day", "Asprin 50mg/day",
                    "Asprin 100mg/day", "Acetomycin 100mg/day",
                    "Acetomycin 1000mg/day")

     # ordinary sort puts the dosages in the wrong order
     sort(Treatment)

     # but mixedsort does the 'right' thing
     mixedsort(Treatment)

     # Here is a more complex example
     x <- rev(c("AA 0.50 ml", "AA 1.5 ml", "AA 500 ml", "AA 1500 ml",
                "EXP 1", "AA 1e3 ml", "A A A", "1 2 3 A", "NA", NA, "1e2",
                "", "-", "1A", "1 A", "100", "100A", "Inf"))

     mixedorder(x)

     mixedsort(x)
     # notice that plain numbers, including 'Inf' show up before strings.

