Re: [R] Factor function: odd behavior when labels argument containsduplicates?

From: <Bill.Venables_at_csiro.au>
Date: Thu, 31 May 2007 15:17:38 +1000

There is a difference between levels and labels. I think this is what you want.

> x <- factor(rep(0:5, 2))
> x

 [1] 0 1 2 3 4 5 0 1 2 3 4 5
Levels: 0 1 2 3 4 5
> levels(x) <- c(1,1:5)
> x
 [1] 1 1 2 3 4 5 1 1 2 3 4 5
Levels: 1 2 3 4 5
> table(x)

x
1 2 3 4 5
4 2 2 2 2  

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
AUSTRALIA
Office Phone (email preferred): +61 7 3826 7251 Fax (if absolutely necessary): +61 7 3826 7304

Mobile:                (I don't have one!)
Home Phone:                     +61 7 3286 7700
mailto:Bill.Venables_at_csiro.au
http://www.cmis.csiro.au/bill.venables/

-----Original Message-----
From: r-help-bounces_at_stat.math.ethz.ch
[mailto:r-help-bounces_at_stat.math.ethz.ch] On Behalf Of Steen Ladelund Sent: Wednesday, 30 May 2007 6:27 PM
To: r-help_at_stat.math.ethz.ch
Subject: [R] Factor function: odd behavior when labels argument containsduplicates?

Hi all.

I think it would be nice to be able to combine levels of a factor on creation a la

 x <- rep(0:5,5)

 y <- factor(x,levels=0:5,labels=c('1','1',2:5)) ## (1)

 y
 [1] 1 1 2 3 4 5 1 1 2 3 4 5 1 1 2 3 4 5 1 1 2 3 4 5 1 1 2 3 4 5 Levels: 1 1 2 3 4 5

I thougt this would (should?) create a factor with 5 levels, ie combining 0 and 1 in x into one level in y.

I find it hard to predict the behavior of the following lines:

 table(factor(y)) ## Odd ?
 1 1 2 3 4 5
10 0 5 5 5 5
 table(factor(factor(y))) ## This is what I want  1 2 3 4 5
10 5 5 5 5

It also seems strange that this should be the way to go:  levels(y) <- levels(y) ## Does what I want following (1) even tough it appear to be a void statement?
 y
y
 [1] 1 1 2 3 4 5 1 1 2 3 4 5 1 1 2 3 4 5 1 1 2 3 4 5 1 1 2 3 4 5 Levels: 1 2 3 4 5

Am I missing an important point about factors or the factor function?

steen

Steen Ladelund, statistician
+4543233275 stelad01_at_glostruphosp.kbhamt.dk Research Center for Prevention and Health Glostrup University Hospital, Denmark
www.fcfs.kbhamt.dk



R-help_at_stat.math.ethz.ch 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_stat.math.ethz.ch 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. Received on Thu 31 May 2007 - 05:25:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 May 2007 - 07:33:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.