Skip to content

elegant way to minus on each row of a matrix

5 messages · Bo Zhou, Dimitris Rizopoulos, Henrique Dallazuanna

#
try this:

mat <- matrix(1:9, 3, 3, TRUE)
dat <- as.data.frame(mat)
vec <- 1:3

result.mat <- mat - vec
result.dat <- dat - vec
result.mat
result.dat


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting Bo Zhou <bozhou1981 at hotmail.com>:
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
Try this:

sweep(mat, 1, vec)
On 02/03/2008, Bo Zhou <bozhou1981 at hotmail.com> wrote: