mapFactor               package:gdata               R Documentation

_G_e_t _a _m_a_p _o_f _l_e_v_e_l_s _i_n _a _f_a_c_t_o_r

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

     'mapFactor' produces a list with information on levels and
     internal integer codes. As such can be conveniently used to store
     factor map when one needs to work with internal codes of a factor
     and later transfrorm back to factor.

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

     mapFactor(x, codes=TRUE, sort=TRUE, drop=FALSE, ...)

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

       x: factor, the object to be mapped

   codes: boolean, create map with internal codes or with levels, look
          into value and examples

    sort: boolean, sort levels for a character, look into details

    drop: boolean, drop unused levels of a factor

     ...: additional arguments for 'sort'

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

     'sort' and '...' arguments provides possibility to "order" levels
     and can only be used for characters and not for factors.

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

     A list with names equal to levels and entries equal to internal
     codes, when 'codes=TRUE', or entries equal to levels otherwise.
     The later case is usefull, when one would like to combine two
     factors with different levels.

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

     Gregor Gorjanc

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

     'factor', 'levels', 'unclass', 'attributes'

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

     ## Example with codes=TRUE
     (f <- factor(letters[c(1, 1, 2, 3, 4, 5, 7, 8, 9, 8, 8, 10)]))
     map <- mapFactor(f)
     int <- as.integer(f)
     fNew <- factor(int)
     levels(fNew) <- map
     fNew

     ## Example with codes=FALSE
     f1 <- factor(f[1:5])
     f2 <- factor(f[5:length(f)])
     map1 <- mapFactor(f1, codes=FALSE)
     map2 <- mapFactor(f2, codes=FALSE)
     map <- c(map1, map2)
     levels(f1) <- map
     levels(f2) <- map
     as.integer(f1)
     as.integer(f2)

     ## x <- unique(map)
     ## names(x) <- unlist(x)

