[R] Factor function: odd behavior when labels argument containsduplicates?
Bill.Venables at csiro.au
Bill.Venables at csiro.au
Thu May 31 07:17:38 CEST 2007
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.
More information about the R-help
mailing list