[Rd] matrix coercion, logical -> character

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Dec 8 19:57:42 MET 2003


On Mon, 8 Dec 2003, Patrick Burns wrote:

> It seems another reason to make the change would be the
> ability to work around the following feature:
> 
>  > lmat <- as.matrix(data.frame(a=c(FALSE, TRUE)))
>  > lmat
>   a     
> 1 "FALSE"
> 2 " TRUE"
>  > mode(lmat) <- "logical"
>  >
>  > lmat
>       a
> 1 FALSE
> 2    NA
> 
> Numeric, character and logical were mentioned.  Complex data
> frames coerce to complex matrices already.

(Yes, complex is numeric for this purpose.  I had checked that out.)

> Apropos of this, it would be good to put a See Also "complex"
> in the "numeric" help file and vice versa.
> 
> Patrick Burns
> 
> Burns Statistics
> patrick at burns-stat.com
> +44 (0)20 8525 0696
> http://www.burns-stat.com
> (home of S Poetry and "A Guide for the Unwilling S User")
> 
> Peter Dalgaard wrote:
> 
> >Prof Brian Ripley <ripley at stats.ox.ac.uk> writes:
> >
> >  
> >
> >>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.
> >>    
> >>
> >
> >and for apply:
> >
> >     If 'X' is not an array but has a dimension attribute, 'apply'
> >     attempts to coerce it to an array via 'as.matrix' if it is
> >     two-dimensional (e.g., data frames) or via 'as.array'.
> >
> >explains why apply(...., which) got in trouble
> >
> >  
> >
> >>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.
> >>    
> >>
> >
> >Yes. That was the direction in which I was hinting. It does seem a bit
> >like a leftover, and applying which() over a set of logical
> >columns is not unlikely to be useful in practice.
> >
> >  
> >
> 
> 
> 
> 

-- 
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