Re: [Rd] apply: new behaviour for factors in R-2.4.0

From: Prof Brian Ripley <>
Date: Mon 25 Sep 2006 - 14:11:10 GMT


This is more complicated than your analysis.

  1. apply takes a matrix as an argument, not a data frame, and so first coerced 'dat' to a character matrix.
  2. unlist is working quite correctly. The issue is array(), which contains as.vector(data). Thus although the result could be a factor matrix, as.vector is coercing it to a character matrix. It might be desirable to return a factor matrix, but we are not going to do that in feature freeze (if ever) and I really don't think it would be what you wanted.

Perhaps the help page should contain an explicit statement that the result will be coerced to a basic vector type by as.vector().

On Mon, 25 Sep 2006, Christoph Buser wrote:

> Dear R-core
> There is a different output for the apply function due to the
> change of unlist as mentioned in the R news.
> Newly, applying as.factor() (or factor()) in
> str(dat <- data.frame(x = 1:10, f1 = gl(2,5,labels = c("A", "B"))))
> (d1 <- apply(dat,2,as.factor))
> newly returns a character matrix while in R-2.3.1 the same
> command resulted in an integer matrix that was consistent (up to
> the ordering of the factor levels) with data.matrix().

That's coincidence -- try x=11:20.

> The change is caused by the change of unlist() that, used for a
> list of factors, newly returns a single factor instead of an
> integer. I am happy with this change, but:
> Is it desirable to change apply so that it does not return a
> character matrix in the example above or include a warning for
> such a case?
> Thank you very much for an answer.
> Regards,
> Christoph Buser
> --------------------------------------------------------------
> Christoph Buser <>
> Seminar fuer Statistik, LEO C13
> ETH Zurich 8092 Zurich SWITZERLAND
> phone: x-41-44-632-4673 fax: 632-1228
> ______________________________________________
> mailing list

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Tue Sep 26 00:17:21 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 28 Sep 2006 - 15:30:10 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.