thanks, I knew about apply, but did not you you can put plus signs with
quotes.  That's a cool tricky,
Mike

On Wed, Dec 5, 2012 at 4:05 PM, arun <smartpink...@yahoo.com> wrote:

> HI,
> In addition to ?sweep(), you can use
>
> apply(-mat,1,`+`,x)
>
> #or
> library(plyr)
> aaply(-mat,1,"+",x)
>
>
> A.K.
>
>
>
>
>
>
> ----- Original Message -----
> From: C W <tmrs...@gmail.com>
> To: Sarah Goslee <sarah.gos...@gmail.com>
> Cc: r-help <r-help@r-project.org>
> Sent: Wednesday, December 5, 2012 1:51 PM
> Subject: Re: [R] data manipulation between vector and matrix
>
> Thanks, Sarah.  First time heard about sweep(), it worked just the way I
> wanted.
> Mike
>
> On Wed, Dec 5, 2012 at 1:42 PM, Sarah Goslee <sarah.gos...@gmail.com>
> wrote:
>
> > Hi,
> >
> > On Wed, Dec 5, 2012 at 1:30 PM, C W <tmrs...@gmail.com> wrote:
> > > Dear list,
> > > I was curious how to subtract a vector from matrix?
> > >
> > > Say, I have
> > >
> > > mat <- matrix(1:40, nrow=20, ncol=2)
> > >
> > > x <-c(1,2)
> >
> > Thanks for the actual reproducible example.
> >
> > > I want,
> > >
> > > x-mat[1,] and x-mat[2,], and so on... Basically, subtract column
> elements
> > > of x against column elements in mat.
> > >
> > > But x-mat won't do it.
> >
> > This will (note the modification to get x - mat):
> > > sweep(-mat, 2, x, "+")
> >       [,1] [,2]
> >  [1,]    0  -19
> >  [2,]   -1  -20
> >  [3,]   -2  -21
> >  [4,]   -3  -22
> >  [5,]   -4  -23
> > etc.
> >
> > --
> > Sarah Goslee
> > http://www.functionaldiversity.org
> >
>
>     [[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.
>
>

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

Reply via email to