[R] sub() of matrix returns a vector and not a matrix

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Tue May 7 16:45:33 CEST 2002


On Tue, 7 May 2002, Martin Maechler wrote:

> >>>>> "Wolfram" == "Wolfram Fischer <- Z/I/M" <wolfram at fischer-zim.ch>> writes:
>
>     Wolfram> Is there a simple possibility to become directly a
>     Wolfram> matrix from a call of sub() on a matrix?
>
> This can be considered as a feature proposal which I would
> second:
>
> sub() and gsub() could be enhanced such as to keep attributes(.)
> of their x argument.
>
> The `dim' attribute which makes a vector into a matrix is just
> one kind of attributes.
>
> Opinions?

Care is needed, as always with attributes (which also include "class", for
example).  As here there is no coercion going on (e.g. data.frame to
matrix or v.v.) preserving dim and dimnames attributes is probably safe,
but class may not be.

>
>
>     >> a <- matrix( letters[1:6], 2, 3 )
>     >> b <- sub( '(.)', '-\\1-', a )
>     Wolfram> 	# I had expected b to be a matrix # but b is a  vector
>     >> print( b )
>     Wolfram> [1] "-a-" "-b-" "-c-" "-d-" "-e-" "-f-"
>
>     >> dim(b) <- dim(a)
>     >> print( b )
>     Wolfram> # b is now a matrix again
>     ....
>
> Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
> Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
> ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
> phone: x-41-1-632-3408		fax: ...-1228			<><
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list