[R] using cut on matrices
Bill.Venables@csiro.au
Bill.Venables at csiro.au
Sun Jul 13 09:21:09 CEST 2003
One solution to this problem is to use a function like
Cut <- function(x, ...) {
m <- match.call()
m[[1]] <- as.name("cut")
v <- eval(m, sys.frame(sys.parent()))
if(is.matrix(x)) matrix(v, nrow(x), ncol(x)) else v
}
but it is an odd sort of thing to want to do. cut(...) in R produces a
factor result. If you feed this back into a matrix
matrix(cut(v, breaks = pretty(v)), nrow(v), ncol(v))
you get a character matrix.
If you adopt Peter's solution below
ac <- cut(a, ......)
dim(ac) <- dim(a)
and a is genuine matrix, you get a factor with a dim attribute which is not
of class matrix. I found this a bit odd, but don't actually class it as a
bug. It appears to be another unforseen consequence of the new classes and
methods, I suspect.
Bill Venables.
-----Original Message-----
From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk]
Sent: Saturday, July 12, 2003 8:43 PM
To: tpapp at axelero.hu
Cc: R-help mailing list
Subject: Re: [R] using cut on matrices
Tamas Papp <tpapp at axelero.hu> writes:
> Dear list,
>
> I'd like to use the function cut() on matrices, ie that when I apply
> it to a matrix, it would return a matrix of the same dimensions
> instead of a vector.
>
> I wonder if there is a better (more elegant) solution than
>
> matrix(cut(a, ...), ncol=ncol(a), nrow=nrow(a))
>
> because I would like to use cut on both vectors and matrices and avoid
> testing whether a is a matrix.
Will this not work?:
ac <- cut(a)
dim(ac) <- dim(a)
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list