Re: [R] data manipulation between vector and matrix

2012-12-05 Thread arun
HI,
The option z5 takes care of it.
z5<-t(x-t(mat)) #still faster than ?sweep()
 dim(z5)
[1] 20  2
 identical(sweep(-mat,2,x,"+"),z5)
#[1] TRUE


A.K.






From: C W 
To: arun  
Sent: Wednesday, December 5, 2012 5:09 PM
Subject: Re: [R] data manipulation between vector and matrix


Hi Arun,
Sorry, I might be a little unclear with my words.

The dimensions are different. This is what I got:
> x-t(mat)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    0   -1   -2   -3   -4   -5   -6   -7   -8    -9   -10   -11   -12   -13
[2,]  -19  -20  -21  -22  -23  -24  -25  -26  -27   -28   -29   -30   -31   -32
     [,15] [,16] [,17] [,18] [,19] [,20]
[1,]   -14   -15   -16   -17   -18   -19
[2,]   -33   -34   -35   -36   -37   -38
> sweep(-mat, 2, x, "+")
      [,1] [,2]
 [1,]    0  -19
 [2,]   -1  -20
 [3,]   -2  -21
 [4,]   -3  -22
 [5,]   -4  -23
 [6,]   -5  -24
 [7,]   -6  -25
 [8,]   -7  -26
 [9,]   -8  -27
[10,]   -9  -28
[11,]  -10  -29
[12,]  -11  -30
[13,]  -12  -31
[14,]  -13  -32
[15,]  -14  -33
[16,]  -15  -34
[17,]  -16  -35
[18,]  -17  -36
[19,]  -18  -37
[20,]  -19  -38
> dim(x-t(mat))
[1]  2 20
> dim(sweep(-mat, 2, x, "+"))
[1] 20  2

On Wed, Dec 5, 2012 at 4:55 PM, arun  wrote:

HI Mike,
>I didn't understand "except the x-t(mat) output is very different than the 
>others".  Are you saying that it needs to be transposed?  BTW, that was z5.
>
>A.K.
>
>
>
>
>
>
>
>
>From: C W 
>To: arun 
>Cc: R help ; Sarah Goslee 
>Sent: Wednesday, December 5, 2012 4:47 PM
>
>Subject: Re: [R] data manipulation between vector and matrix
>
>
>Thanks for the benchmark.  I actually wanted to go with the winner, except the 
>x-t(mat) output is very different than the others.
>Mike
>
>
>On Wed, Dec 5, 2012 at 4:40 PM, arun  wrote:
>
>Hi,
>>
>>By comparing the different methods:
>>set.seed(5)
>> mat1<-matrix(sample(1:1e6,1e6,replace=TRUE),ncol=1)
>> set.seed(25)
>> x<-sample(1:1e6,1,replace=TRUE)
>> system.time(z1<-sweep(-mat1,2,x,"+"))
>>#   user  system elapsed
>> # 0.076   0.000   0.069
>> system.time(z2<-apply(-mat1,1,`+`,x))
>> #  user  system elapsed
>> # 0.036   0.000   0.031
>> system.time(z3<-aaply(-mat1,1,`+`,x))
>>#   user  system elapsed
>>#  1.880   0.000   1.704
>> system.time(z4<- x-t(mat1))  #winner
>>#   user  system elapsed
>> # 0.004   0.000   0.007
>> system.time(z5<- t(x-t(mat1)))
>>#   user  system elapsed
>>#  0.008   0.000   0.009
>>
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>
>>From: C W 
>>To: arun 
>>Cc: R help ; Sarah Goslee 
>>Sent: Wednesday, December 5, 2012 4:11 PM
>>
>>Subject: Re: [R] data manipulation between vector and matrix
>>
>>
>>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  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 
>>>To: Sarah Goslee 
>>>Cc: r-help 
>>>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  wrote:
>>>
>>>> Hi,
>>>>
>>>> On Wed, Dec 5, 2012 at 1:30 PM, C W  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.
>>>
>>>
>>
>

__
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] data manipulation between vector and matrix

2012-12-05 Thread arun
Hi,

By comparing the different methods:
set.seed(5)
 mat1<-matrix(sample(1:1e6,1e6,replace=TRUE),ncol=1)
 set.seed(25)
 x<-sample(1:1e6,1,replace=TRUE)
 system.time(z1<-sweep(-mat1,2,x,"+"))
#   user  system elapsed 
 # 0.076   0.000   0.069 
 system.time(z2<-apply(-mat1,1,`+`,x))
 #  user  system elapsed 
 # 0.036   0.000   0.031 
 system.time(z3<-aaply(-mat1,1,`+`,x))
#   user  system elapsed 
#  1.880   0.000   1.704 
 system.time(z4<- x-t(mat1))  #winner
#   user  system elapsed 
 # 0.004   0.000   0.007 
 system.time(z5<- t(x-t(mat1)))
#   user  system elapsed 
#  0.008   0.000   0.009 


A.K.






From: C W 
To: arun  
Cc: R help ; Sarah Goslee  
Sent: Wednesday, December 5, 2012 4:11 PM
Subject: Re: [R] data manipulation between vector and matrix


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  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 
>To: Sarah Goslee 
>Cc: r-help 
>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  wrote:
>
>> Hi,
>>
>> On Wed, Dec 5, 2012 at 1:30 PM, C W  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.
>
>

__
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] data manipulation between vector and matrix

2012-12-05 Thread C W
Thanks for the benchmark.  I actually wanted to go with the winner, except
the x-t(mat) output is very different than the others.
Mike

On Wed, Dec 5, 2012 at 4:40 PM, arun  wrote:

> Hi,
>
> By comparing the different methods:
> set.seed(5)
>  mat1<-matrix(sample(1:1e6,1e6,replace=TRUE),ncol=1)
>  set.seed(25)
>  x<-sample(1:1e6,1,replace=TRUE)
>  system.time(z1<-sweep(-mat1,2,x,"+"))
> #   user  system elapsed
>  # 0.076   0.000   0.069
>  system.time(z2<-apply(-mat1,1,`+`,x))
>  #  user  system elapsed
>  # 0.036   0.000   0.031
>  system.time(z3<-aaply(-mat1,1,`+`,x))
> #   user  system elapsed
> #  1.880   0.000   1.704
>  system.time(z4<- x-t(mat1))  #winner
> #   user  system elapsed
>  # 0.004   0.000   0.007
>  system.time(z5<- t(x-t(mat1)))
> #   user  system elapsed
> #  0.008   0.000   0.009
>
>
> A.K.
>
>
>
>
>
> ________
> From: C W 
> To: arun 
> Cc: R help ; Sarah Goslee 
> Sent: Wednesday, December 5, 2012 4:11 PM
> Subject: Re: [R] data manipulation between vector and matrix
>
>
> 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  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 
> >To: Sarah Goslee 
> >Cc: r-help 
> >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 
> wrote:
> >
> >> Hi,
> >>
> >> On Wed, Dec 5, 2012 at 1:30 PM, C W  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.


Re: [R] data manipulation between vector and matrix

2012-12-05 Thread arun
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 
To: Sarah Goslee 
Cc: r-help 
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  wrote:

> Hi,
>
> On Wed, Dec 5, 2012 at 1:30 PM, C W  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.


__
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] data manipulation between vector and matrix

2012-12-05 Thread C W
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  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 
> To: Sarah Goslee 
> Cc: r-help 
> 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 
> wrote:
>
> > Hi,
> >
> > On Wed, Dec 5, 2012 at 1:30 PM, C W  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.


Re: [R] data manipulation between vector and matrix

2012-12-05 Thread C W
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  wrote:

> Hi,
>
> On Wed, Dec 5, 2012 at 1:30 PM, C W  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.


Re: [R] data manipulation between vector and matrix

2012-12-05 Thread Sarah Goslee
Hi,

On Wed, Dec 5, 2012 at 1:30 PM, C W  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

__
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] data manipulation between vector and matrix

2012-12-05 Thread C W
The only solution I found was
x-t(mu)

Is there a better way?
Mike

On Wed, Dec 5, 2012 at 1:30 PM, C W  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)
>
> 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.
>
> Thanks,
>
> Mike
>
>

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


[R] data manipulation between vector and matrix

2012-12-05 Thread C W
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)

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.

Thanks,

Mike

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