Re: [R] elegant way to minus on each row of a matrix

From: Bo Zhou <bozhou1981_at_hotmail.com>
Date: Sun, 02 Mar 2008 18:14:10 +0000

Hi Dimitris and everyone

I tried this but now I know why it didn't work out for me initially.

> t1=matrix(1:10,5,2)
> t2=matrix(1,5,1)
> t2

     [,1]

[1,]    1
[2,]    1
[3,]    1
[4,]    1
[5,]    1

> t1
[,1] [,2] [1,] 1 6 [2,] 2 7 [3,] 3 8

[4,] 4 9
[5,] 5 10
> t1-as.vector(t2)

     [,1] [,2]

[1,]    0    5
[2,]    1    6
[3,]    2    7
[4,]    3    8
[5,]    4    9

> t1-t2

Error in t1 - t2 : non-conformable arrays

I'm too used to Matlab I guess.

Cheers,

Bo

> Date: Sun, 2 Mar 2008 18:56:15 +0100
> From: Dimitris.Rizopoulos_at_med.kuleuven.be
> To: bozhou1981@hotmail.com
> CC: r-help_at_r-project.org
> Subject: Re: [R] elegant way to minus on each row of a matrix
>
> 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>:
>
> >
> > How to do this in an elegant way formatrix/data frame/zoo?
> >
> > mat=
> > 1 2 3
> > 4 5 6
> > 7 8 9
> >
> > vector=
> > 1
> > 2
> > 3
> >
> >
> > result=
> > 0 1 2
> > 2 3 4
> > 4 5 6
> >
> > ie
> > 1-1 2-1 3-1
> > 4-2 5-2 6-2
> > 7-3 8-3 9-3
> >
> > Thanks in advance.
> >
> > _________________________________________________________________
> >
> >
> > 08
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help_at_r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
>
>
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>


08

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 02 Mar 2008 - 18:19:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 02 Mar 2008 - 18:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive