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:01:12 +0000

Hi Mark, (CC'ing r-help)

Only need to change the parameter from 1 to 2 after that it worked great. Thanks

> t1=matrix(1:15,5,3)

> t1

     [,1] [,2] [,3]

[1,] 1 6 11

[2,] 2 7 12

[3,] 3 8 13

[4,] 4 9 14

[5,] 5 10 15

> t2=1:5

> t2

[1] 1 2 3 4 5

> apply(t1,2, function(x) x - t2)

     [,1] [,2] [,3]

[1,] 0 5 10

[2,] 0 5 10

[3,] 0 5 10

[4,] 0 5 10

[5,] 0 5 10

>

> Date: Sun, 2 Mar 2008 12:51:53 -0500
> From: markleeds_at_verizon.net
> Subject: RE: [R] elegant way to minus on each row of a matrix
> To: bozhou1981_at_hotmail.com
>
> try result<-apply(mat,1, function(.row) .row - vector) but I don't have R
> here so make sure it works.
>
> -----Original Message-----
> From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On
> Behalf Of Bo Zhou
> Sent: Sunday, March 02, 2008 12:43 PM
> To: r-help_at_r-project.org
> Subject: [R] elegant way to minus on each row of a matrix
>
>
> 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.
>


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:04:20 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