I am looking into change the numeric order in the level of the factor> x<-c("A","B","C") > fx<-factor(x) > fx[1] A B C Levels: A B C> factor(x)[1] A B C Levels: A B C> as.numeric(fx)[1] 1 2 3 I want to change the order of the numeric into 3 corresponds to ?A? level, 2 corresponds to ?B? level and 1 corresponds to ?C? level So when I type in As.numeric(fx) I would like to get 3 2 1 Your help is greatly appreciated -- View this message in context: http://www.nabble.com/factor-tp21822571p21822571.html Sent from the R help mailing list archive at Nabble.com.
On 4/02/2009, at 2:09 PM, kayj wrote:> > I am looking into change the numeric order in the level of the factor > >> x<-c("A","B","C") >> fx<-factor(x) >> fx > [1] A B C > Levels: A B C >> factor(x) > [1] A B C > Levels: A B C >> as.numeric(fx) > [1] 1 2 3 > > I want to change the order of the numeric into 3 corresponds to ?A? > level, 2 > corresponds to ?B? level and 1 corresponds to ?C? level > > So when I type in > > As.numeric(fx) I would like to get > > 3 2 1fx <- factor(x,levels=c("C","B","A")) cheers, Rolf Turner ###################################################################### Attention: This e-mail message is privileged and confidential. If you are not the intended recipient please delete the message and notify the sender. Any views or opinions presented are solely those of the author. This e-mail has been scanned and cleared by MailMarshal www.marshalsoftware.com ######################################################################
kayj wrote:> I am looking into change the numeric order in the level of the factor > >> x<-c("A","B","C") >> fx<-factor(x) >> fx > [1] A B C > Levels: A B C >> factor(x) > [1] A B C > Levels: A B C >> as.numeric(fx) > [1] 1 2 3 > > I want to change the order of the numeric into 3 corresponds to ?A? level, 2 > corresponds to ?B? level and 1 corresponds to ?C? level > > So when I type in > > As.numeric(fx) I would like to get > > 3 2 1 > > Your help is greatly appreciated >fx <- factor(x, levels=c("C","B","A")) -- 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
Thanks for the tip, it worked, but I was wondering if I can use a vector of variables so I tried mode (l) ?List?> l[3,1][1] A Levels: A ,B , C>l[2,1][1] B Levels: A ,B , C>l[1,1][1] C Levels: A ,B , C I am trying to use the variable name so I tried fx<-factor(x,levels=c(l[3,1],l[2,1],l[1,1])) and that did not work, how can I use variables to create a vector? kayj wrote:> > I am looking into change the numeric order in the level of the factor > >> x<-c("A","B","C") >> fx<-factor(x) >> fx > [1] A B C > Levels: A B C >> factor(x) > [1] A B C > Levels: A B C >> as.numeric(fx) > [1] 1 2 3 > > I want to change the order of the numeric into 3 corresponds to ?A? level, > 2 corresponds to ?B? level and 1 corresponds to ?C? level > > So when I type in > > As.numeric(fx) I would like to get > > 3 2 1 > > Your help is greatly appreciated > >-- View this message in context: http://www.nabble.com/factor-tp21822571p21837498.html Sent from the R help mailing list archive at Nabble.com.