reorder.factor           package:gregmisc           R Documentation

_R_e_o_r_d_e_r _t_h_e _L_e_v_e_l_s _o_f _a _F_a_c_t_o_r

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

     Reorder the levels of a factor

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

     ## S3 method for class 'factor':
     reorder(x, order, ...)

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

       x: factor.

   order: Vector of indexes or label names for new factor ordering.

     ...: Optional parameters (ignored.)

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

     When 'order' is a numeric vector, this function is simply a
     convenence wrapper for 'factor(x,levels=levels(x)[order])'.
     Otherwise this functionis simply a wrapper for
     'factor(x,levels=order)'.

     I find this function useful for reordering factors so that the
     default treatment contrasts use the appropriate levels for
     comparison.

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

     A new factor with the levels ordered as specified.  Note that
     levels not specified 'order' will become missing values.

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

     Gregory R. Warnes Gregory_R_Warnes@groton.pfizer.com

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

     'factor', 'reorder'

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

        # Create a 4 level example factor
        trt <- factor( sample( c("PLACEBO","300 MG", "600 MG", "1200 MG"),
                       100, replace=TRUE ) )
        summary(trt)
        # Note that the levels are not in a meaningful order.

        # Change the order to something useful
        # using indexes:
        trt2 <- reorder(trt, c(4,2,3,1))
        summary(trt2)
        # using label names:
        trt2 <- reorder(trt, c("PLACEBO","300 MG", "600 MG", "1200 MG") )
        summary(trt2)

        # drop out the '300 MG' level
        trt3 <- reorder(trt, c("PLACEBO", "600 MG", "1200 MG") )
        summary(trt3)

