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 ###################################################################### Attention:\ This e-mail message is privileged and confidenti...{{dropped}}
Would levels(fff) <- c("A","S","U") not work? Can you send an example? -Christos> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Rolf Turner > Sent: Wednesday, October 03, 2007 12:58 AM > To: r-help list > Subject: [R] Factor levels. > > > 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 > > > ###################################################################### > Attention:\ This e-mail message is privileged and > confidenti...{{dropped}} > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >
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. >Does it even work? (What if it is the first or the 2nd level that is absent?) The canonical way is factor(fff, levels=c("Unit", "Achieved", "Scholarship"), labels=c("U","A","S"))> Is there a sexier way? > > cheers, > > Rolf Turner >-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
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 TurnerMaybe 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 ------------------------------