levels.factor              package:base              R Documentation

_F_a_c_t_o_r _L_e_v_e_l_s _A_s_s_i_g_n_m_e_n_t

_D_e_s_c_r_i_p_t_i_o_n:

     `levels<-' provides a way to alter the levels attribute of factor.
     `value' can be a vector of character strings with length at least
     the the number of levels of `x', or a named list specifying how to
     rename the levels.

_U_s_a_g_e:

     levels(x) <- value

_S_e_e _A_l_s_o:

     `factor', `levels', `levels<-', `nlevels'.

_E_x_a_m_p_l_e_s:

     # assign individual levels
     x <- gl(2, 4, 8)
     levels(x)[1] <- "low"
     levels(x)[2] <- "high"
     x

     # or as a group
     y <- gl(2, 4, 8)
     levels(y) <- c("low", "high")
     y

     # combine some levels
     z <- gl(3, 2, 12)
     levels(z) <- c("A", "B", "A")
     z

     # same, using a named list
     z <- gl(3, 2, 12)
     levels(z) <- list(A=c(1,3), B=2)
     z

     # we can add levels this way:
     f <- factor(c("a","b"))
     levels(f) <- c("c", "a", "b")
     f

     f <- factor(c("a","b"))
     levels(f) <- list(C="C", A="a", B="b")
     f

