[R] Renaming levels of a factor in a dataframe

Petr PIKAL petr.pikal at precheza.cz
Mon Aug 15 09:18:06 CEST 2011


Hi
> 
> Here's an example of relevel used to relevel and combine groups....
> 
> InsectSprays2<-InsectSprays
> levels(InsectSprays2$spray)
> 
levels(InsectSprays2$spray)<-list(new1=c("A","C"),YEPS=c("B","D","E"),LASTLY="F")
> levels(InsectSprays2$spray)
> InsectSprays2
> 
> So for you try...
> levels (Data1$Site) <- list(Fw =c( "AB"), Est = c("DE"))

Or simply 

levels (Data1$Site) <- c("Fw", "Est")

it is also pretty easy to change only some levels

> x.f
  [1] c a a e a d a d d e a c c c e c c a a b d b d b a e b a b a b d e e 
c b b
 [38] d c a d b a d e b d c e e d e a d e b c a d d c c b a a e c c c d c 
a d c
 [75] a a b a c c a e d d d e a e d d c a c c c d e a c d
Levels: a b c d e
> levels(x.f)[1:3]<-"A"
> x.f
  [1] A A A e A d A d d e A A A A e A A A A A d A d A A e A A A A A d e e 
A A A
 [38] d A A d A A d e A d A e e d e A d e A A A d d A A A A A e A A A d A 
A d A
 [75] A A A A A A A e d d d e A e d d A A A A A d e A A d
Levels: A d e
> levels(x.f)[2:3]<-c("x", "y")
> x.f
  [1] A A A y A x A x x y A A A A y A A A A A x A x A A y A A A A A x y y 
A A A
 [38] x A A x A A x y A x A y y x y A x y A A A x x A A A A A y A A A x A 
A x A
 [75] A A A A A A A y x x x y A y x x A A A A A x y A A x
Levels: A x y
>

Regards
Petr


>

> 
> 
> > From: deelman at hotmail.com
> > To: r-help at r-project.org
> > Date: Sun, 14 Aug 2011 12:56:25 -0300
> > Subject: [R] Renaming levels of a factor in a dataframe
> > 
> > 
> > 
> > Dear Helplist:
> > 
> > 
> > 
> > I am trying, unsuccessfully, to rename levels of a factor in a 
> dataframe. The dataframe consists of two factor variables and one 
numeric 
> variable as follows:
> > 
> > Factor Site has 2 levels AB and DE, factor Fish has 30 levels, 15 
> associated with each Site e.g. 1-1, 1-2,.....2-1, 2-2.... I am trying to 

> rename the levels of factor Site from AB to Fw and DE to Est while 
keeping
> them as factors. The following 2 approaches do not work, each giving a 
> NULL response and creating a character string. 
> > 
> > 
> > 
> > levels (Data1$Site <- c("Fw", "Est")) This simply gives an alternating 

> list of Fw, Est, Fw, Est... not the desired 15 concurrent rows of Fw 
> followed by 15 of Est.
> > 
> > 
> > 
> > #levels (Data1$Site <- list(Fw = "AB", Est = "DE")) This gives the 
same 
> result. I have tried other approaches to no avail. It seems a simple 
> problem but has not been so. 
> > 
> > 
> > 
> > Any suggestions for solving this problem would be much appreciated.
> > 
> > 
> > 
> > Regards,
> > 
> > BJ 
> > ______________________________________________
> > 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.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list