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

2008-03-02 Thread Bo Zhou

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@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.


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

2008-03-02 Thread Bo Zhou

Brilliant! 

> t1=matrix(1:15,5,3)
> t1
 [,1] [,2] [,3]
[1,]16   11
[2,]27   12
[3,]38   13
[4,]49   14
[5,]5   10   15
> t2=1:5
> t2
[1] 1 2 3 4 5

> sweep(t1, 1, t2)
 [,1] [,2] [,3]
[1,]05   10
[2,]05   10
[3,]05   10
[4,]05   10
[5,]05   10

> Date: Sun, 2 Mar 2008 14:56:22 -0300
> From: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> Subject: Re: [R] elegant way to minus on each row of a matrix
> CC: r-help@r-project.org
> 
> Try this:
> 
> sweep(mat, 1, vec)
> 
> 
> On 02/03/2008, Bo Zhou <[EMAIL PROTECTED]> wrote:
> >
> > 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@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.
> >
> 
> 
> -- 
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O

_
Need to know the score, the latest news, or you need your Hotmail®-get your 
"fix".

[[alternative HTML version deleted]]

__
R-help@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.


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

2008-03-02 Thread Bo Zhou

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,]16
[2,]27
[3,]38
[4,]49
[5,]5   10
> t1-as.vector(t2)
 [,1] [,2]
[1,]05
[2,]16
[3,]27
[4,]38
[5,]49
> 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: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> CC: r-help@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 <[EMAIL PROTECTED]>:
> 
> >
> > 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@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@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.


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

2008-03-02 Thread Henrique Dallazuanna
Try this:

sweep(mat, 1, vec)


On 02/03/2008, Bo Zhou <[EMAIL PROTECTED]> wrote:
>
> 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@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.
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

__
R-help@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.


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

2008-03-02 Thread Bo Zhou

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,]16   11

[2,]27   12

[3,]38   13

[4,]49   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,]05   10

[2,]05   10

[3,]05   10

[4,]05   10

[5,]05   10

> 

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


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

2008-03-02 Thread Dimitris Rizopoulos
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 <[EMAIL PROTECTED]>:

>
> 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@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

__
R-help@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.