[R] Idiom for column operations?
sundar.dorai-raj at PDF.COM
Tue Oct 26 23:08:52 CEST 2004
Rob Steele wrote:
> Is there a better way to express operations between matrices and column
> vectors than transposing the matrix twice?
> This is the kind of thing I'm talking about:
> m = matrix(1:20, 3, 4)
> v = colSums(m)
> t(t(m) / v) ## <-- kinda ugly, ain't it?
> I thought of converting the column vector to a matrix:
> m / matrix(v, nrow = nrow(m), ncol = length(v), byrow = TRUE)
> But that seems even worse.
> Rob Steele
Would ?sweep be sufficient for you?
m <- matrix(1:20, 5, 4)
sweep(m, 2, colSums(m), "/")
More information about the R-help