[R] Factor function: odd behavior when labels argument contains duplicates?

From: Steen Ladelund <STELAD01_at_glo.regionh.dk>
Date: Wed, 30 May 2007 10:26:32 +0200


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. Received on Wed 30 May 2007 - 08:32: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 - 06:31:28 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.