From ripley at stats.ox.ac.uk  Mon Dec  8 17:12:38 2003
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon Dec  8 17:11:19 2003
Subject: [Rd] matrix coercion, logical -> character
In-Reply-To: <x2smjvpaeq.fsf@biostat.ku.dk>
Message-ID: <Pine.LNX.4.44.0312081607300.19904-100000@gannet.stats>

On 8 Dec 2003, Peter Dalgaard wrote:

> 
> Anyone know whether this is intentional, and by which rationale?
> (R-devel on RedHat, but hardly new)

It is documented:

     'as.matrix' is a generic function. The method for data frames will
     convert any non-numeric column into a character vector using
     'format' and so return a character matrix.

Remember than not some long ago you could not have logical columns in data 
frames: they were coerced to factors.

It would be easy to change to allow numeric, logical or character 
matrices.



> 
> 
> > l <- data.frame(a=rpois(10,1)>0,b=rpois(10,1)>0)
> > apply(l,1,which)
> Error in FUN(newX[, i], ...) : argument to "which" is not logical
> > lapply(l,mode)
> $a
> [1] "logical"
> 
> $b
> [1] "logical"
> 
> > mode(as.matrix(l))
> [1] "character"
> 
> 
> 

-- 
Brian D. Ripley,                  ripley@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


[Rd] matrix coercion, logical -> character
Prof Brian Ripley