[R] FW: Renaming levels of a factor in a dataframe

Bert Gunter gunter.berton at gene.com
Mon Aug 15 16:54:38 CEST 2011


Well, it looks like the standard advice here applies to both poster
and responder: try reading the Help (carefully): ?factor

Example:

> z <- factor(letters[1:3])
> z
[1] a b c
Levels: a b c
> factor(z,labels=letters[4:6])
[1] d e f
Levels: d e f
> factor(z,labels=letters[3:1])
[1] c b a
Levels: c b a
>
Does this not answer your query?
For a slick way of how to do this within a list, see ?within

-- Bert


On Mon, Aug 15, 2011 at 7:36 AM, B Jessop <deelman at hotmail.com> wrote:
>
> Tyler,
>
>
>
> Tried your solution: levels (Data1$Site <- list(Fw = c("AB"), Est = c("DE")))
>
>
>
> but still got a NULL response to str(Data1) and an alternating list of Fw, Est, Fw, Est under Site when looked at in the Data editor in R console. The use of the "levels" function would seem to be appropriate but tricky to use.  Any other ideas would be welcome.
>
>
>
> Regards,
>
> BJ
>
> ________________________________
>> From: tyler_rinker at hotmail.com
>> To: deelman at hotmail.com; r-help at r-project.org
>> Subject: RE: [R] Renaming levels of a factor in a dataframe
>> Date: Sun, 14 Aug 2011 13:18:12 -0400
>>
>> 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"))
>>
>> > 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.
> ______________________________________________
> 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.
>



-- 
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics



More information about the R-help mailing list