[R] Factor names & levels

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Dec 22 02:11:25 CET 2003


Damon Wischik <djw1005 at cam.ac.uk> writes:

> I still want to know how I should understand the following:
> 
> > x <- factor(c("one","three"))
> > names(x) <- c("fred","jim")
> > names(x)
> [1] "fred" "jim" 
> class(x)
> [1] "factor"
> 
> Given that names seems to work on factors, I can see two possibilities:
> 1. It is a bug that it acts as it does;
> 2. the default method does what it says in the help page, but also does
> more than just this.

The behaviour is *undefined*, for crying out loud! That means that if
you try to do it anyway, you may get an error, something
semi-sensible, or find that the code transfers a large sum of money to
a Danish bank account...

In the present implementation, the names attribute gets attached to
the factor just as to an integer vector, but it is not shown when
printing and there are several parts of the code where no attempt is
made to preserve names since there are not expected to be any. 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list