Re: [R] Using different function (parameters) with apply

2013-04-18 Thread andrija djurovic
Hi.

It would be easier to help you if you show us the result you are trying to
obtain.

Here are my attempts:

 sweep(a,2,div, FUN=/)
 [,1] [,2] [,3]
[1,]1  2.0 2.33
[2,]2  2.5 2.67
[3,]3  3.0 3.00

or

 a/matrix(rep(div,3),ncol=3, byrow=TRUE)
 [,1] [,2] [,3]
[1,]1  2.0 2.33
[2,]2  2.5 2.67
[3,]3  3.0 3.00

Hope this helps.

Andrija




On Thu, Apr 18, 2013 at 7:20 AM, Sachinthaka Abeywardana 
sachin.abeyward...@gmail.com wrote:

 Hi All,

 I have the following problem (read the commented bit below):

 a-matrix(1:9,nrow=3)


 a

  [,1] [,2] [,3]
 [1,]147
 [2,]258
 [3,]369


 div-1:3

 apply(a,2,function(x)x/div) ##want to divide each column by div-
 instead each row is divided##


  [,1] [,2] [,3]
 [1,]1  4.07
 [2,]1  2.54
 [3,]1  2.03


 apply(a,1,function(x)x/div) ##Changing Margin from 2 to 1 does
 something completele weird## [,1] [,2] [,3]
 [1,] 1.00 2.003
 [2,] 2.00 2.503
 [3,] 2.33 2.673


 Any thoughts?


 Thanks,

 Sachin

 [[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] Using different function (parameters) with apply

2013-04-18 Thread Berend Hasselman

On 18-04-2013, at 07:20, Sachinthaka Abeywardana sachin.abeyward...@gmail.com 
wrote:

 Hi All,
 
 I have the following problem (read the commented bit below):
 
 a-matrix(1:9,nrow=3)
 
 
 a
 
 [,1] [,2] [,3]
 [1,]147
 [2,]258
 [3,]369
 
 
 div-1:3
 
 apply(a,2,function(x)x/div) ##want to divide each column by div-
 instead each row is divided##
 
 
 [,1] [,2] [,3]
 [1,]1  4.07
 [2,]1  2.54
 [3,]1  2.03
 

column 1 is c(1,2,3) and divided by your vector div gives the first column 
c(1/1,2/2,3/3) = c(1,1,1) and that is correct.
column 3 is c(7,8,9) and divided by vector div gives c(7/1, 8/2 , 9/3) = 
c(7,4,3). Also correct.


 apply(a,1,function(x)x/div) ##Changing Margin from 2 to 1 does
 something completele weird## [,1] [,2] [,3]
 [1,] 1.00 2.003
 [2,] 2.00 2.503
 [3,] 2.33 2.673
 

You are dividing each row by vector div). The result for row 1 is the first 
column and so forth.
Probably if you did t(…) you would get what you want/expect.

 Any thoughts?
 

Your conclusions are incorrect.
What is the result that you want (or expect)?

Berend

__
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] Using different function (parameters) with apply

2013-04-18 Thread David Winsemius

On Apr 17, 2013, at 10:20 PM, Sachinthaka Abeywardana wrote:

 Hi All,
 
 I have the following problem (read the commented bit below):
 
 a-matrix(1:9,nrow=3)
 
 
 a
 
 [,1] [,2] [,3]
 [1,]147
 [2,]258
 [3,]369
 
 
 div-1:3
 
 apply(a,2,function(x)x/div) ##want to divide each column by div-
 instead each row is divided##

No. Each column is divided by that vector 'div'. Try:

1:3/1:3
4:6/1:3
7:9/1:3

 [,1] [,2] [,3]
 [1,]1  4.07
 [2,]1  2.54
 [3,]1  2.03
 
 
 apply(a,1,function(x)x/div) ##Changing Margin from 2 to 1 does
 something completele weird## [,1] [,2] [,3]
 [1,] 1.00 2.003
 [2,] 2.00 2.503
 [3,] 2.33 2.673

Each row is divided by that vector but then returned as columns. Try it with a 
matrix that has a different number of columns than rows.

-- 

David Winsemius
Alameda, CA, USA

__
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] Using different function (parameters) with apply

2013-04-17 Thread Sachinthaka Abeywardana
Hi All,

I have the following problem (read the commented bit below):

a-matrix(1:9,nrow=3)


a

 [,1] [,2] [,3]
[1,]147
[2,]258
[3,]369


div-1:3

apply(a,2,function(x)x/div) ##want to divide each column by div-
instead each row is divided##


 [,1] [,2] [,3]
[1,]1  4.07
[2,]1  2.54
[3,]1  2.03


apply(a,1,function(x)x/div) ##Changing Margin from 2 to 1 does
something completele weird## [,1] [,2] [,3]
[1,] 1.00 2.003
[2,] 2.00 2.503
[3,] 2.33 2.673


Any thoughts?


Thanks,

Sachin

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