[Rd] changing the mode of a factor (PR#9675)

ripley at stats.ox.ac.uk ripley at stats.ox.ac.uk
Mon May 14 13:37:02 CEST 2007

  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-1; FORMAT=flowed
Content-Transfer-Encoding: 8BIT
Content-ID: <Pine.LNX.4.64.0705141227241.28940 at gannet.stats.ox.ac.uk>

How about the following NEWS item (a bug fix)

     o	Assigning class "factor" to an object now requires it has
 	integer (and not say double) codes.

?  (Changing the mode to double amounted to creating a new double vector 
and then assigning the attributes of the old object including the class.
We gave a more specific error message in this case in an attempt to be 
helpful ....)

>From ?factor

      'factor' returns an object of class '"factor"' which has a set of
      integer codes the length of 'x' with a '"levels"' attribute of
      mode 'character'.  If 'ordered' is true (or 'ordered' is used) the
      result has class 'c("ordered", "factor")'.

So a factor necessarily has integer codes.  That you were able to change 
the mode in earlier versions of R was a bug that has been fixed.

You may well want one of the answers to FAQ 7.10.

Please take note of the 'BUGS' section in the FAQ and the posting guide: 
three incorrect bug items in one day is unhelpful.

On Fri, 11 May 2007, frederic.gosselin at cemagref.fr wrote:

> Full_Name: Frédéric Gosselin
> Version: 2.5 vs 2.4 and 2.2.1
> OS: windows XP
> Submission from: (NULL) (
> #############
> L3 <- LETTERS[1:3]
> (d <- data.frame(cbind(x=1, y=1:10), fac=as.factor(sample(L3, 10,
> repl=TRUE))))
> mode(d[,3])<-"numeric"
> ###############
> when trying to change the mode of a column in a data.frame that is a factor to
> numeric(cf. preceding code), the following error occurs (in French) in R 2.5:
> Erreur dans `storage.mode<-`(`*tmp*`, value = "double") :
>        le mode de stockage d'un objet "factor" ne peut être modifié
> The same thing works fine in R2.4 and 2.2.1.
> It does not seem to correspond to new things announced in the NEWS file for the
> 2.5 version...
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list