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

Steen Ladelund STELAD01 at glo.regionh.dk
Wed May 30 10:26:32 CEST 2007


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



More information about the R-help mailing list