[R] matrix
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Sun Apr 23 21:36:09 CEST 2000
On Sun, 23 Apr 2000, gb wrote:
>
> Is there a reason for the following behaviour?
>
> > x <- matrix(1:6, ncol = 1)
> > x
> [,1]
> [1,] 1
> [2,] 2
> [3,] 3
> [4,] 4
> [5,] 5
> [6,] 6
> > is.matrix(x)
> [1] TRUE
> > is.matrix(x[1:2,])
> [1] FALSE
Yes, S-compability! If you want to subset matrices and always get a matrix
you need x[1:2, , drop=FALSE] (note the two commas). Peversely, S drops
dimensions on subsetting but not on matrix multiplication (use drop there).
[This *is* well-documented in the S literature.]
> The reason for asking is that I have a function which takes a matrix as
> an argument, and in the function I "sweep" columns of subsets of the rows
> of the matrix. This works fine, except when the input matrix has only one
> column. Then I get
>
> > sweep(cc[2:3, ], 2, cc.mean)
> Error in aperm(array(STATS, dims[perm]), order(perm)) :
> perm has incorrect length
Um, that does not have only one column, it is a vector and does not have
any columns.
I notice that sweep in R has aped S, but 1:length(dims) is a common error
for seq(along=dims). Perhaps x in sweep should be coerced to an array, or
stop with an informative message (as apply does).
--
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