# [R] Factor levels.

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Oct 3 08:40:58 CEST 2007

```On 03-Oct-07 04:57:33, Rolf Turner wrote:
> I have factors with levels ``Unit", "Achieved", and "Scholarship"; I
> wish to replace these with
> "U", "A", and "S".
>
> So I do
>
>       fff <- factor(fff,labels=c("U","A","S"))
>
> This works as long as all of the levels are actually present in the
> factor.  But if ``Scholarship'' is absent
> (as if often is) then I get an error.
>
> I can do a workaround such as
>
>       fff <- factor(c("U","A","S")[fff],levels=c("U","A","S"))
>
> but this seems kludgy to me.
>
> Is there a sexier way?
>                       cheers,
>                               Rolf Turner

Maybe that particular issue could be worked round with something
derived (e.g. take the leading character in the strings) from

labels=unique(fff)

but that doesn't work round what seems to be a loose specification
of the correspondence between "U" and "Unit", "A" and "Achieved",
"S" and "Scholarship", in that factor(fff), unless told otherwise,
will see the levels in the order "Achieved", "Scholarship", "Unit"
but take the labels in the order "U", "A", "S".

For example, the following mimics your description but with
shorter strings, starting with a vector of "data":

> fff0<-rep(c("Uu","Aa","Ss"),5)

> factor(fff0)
[1] Uu Aa Ss Uu Aa Ss Uu Aa Ss Uu Aa Ss Uu Aa Ss
Levels: Aa Ss Uu

> factor(fff0,labels=c("U","A","S"))
[1] S U A S U A S U A S U A S U A
Levels: U A S

so now U->Aa, A->Ss, S->Uu

Maybe it would be enough for you to use

labels=sort(substr(unique(fff0),1,1))

so that for:

> fff0<-rep(c("Uu","Aa","Ss"),5)

> factor(fff0)
[1] Uu Aa Ss Uu Aa Ss Uu Aa Ss Uu Aa Ss Uu Aa Ss
Levels: Aa Ss Uu

> factor(fff0,labels=sort(substr(unique(fff0),1,1)))
[1] U A S U A S U A S U A S U A S
Levels: A S U

while for:

> fff0<-rep(c("Uu","Aa",),8)
> factor(fff0)
[1] Uu Aa Uu Aa Uu Aa Uu Aa Uu Aa Uu Aa Uu Aa Uu Aa
Levels: Aa Uu
> factor(fff0,labels=sort(substr(unique(fff0),1,1)))
[1] U A U A U A U A U A U A U A U A
Levels: A U

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 03-Oct-07                                       Time: 07:40:54
------------------------------ XFMail ------------------------------

```