[R] VERY SIMPLE QUESTION

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Apr 16 19:51:12 CEST 2010


On Fri, 2010-04-16 at 09:30 -0800, Kathie wrote:
> Dear R users,
> 
> I am looking for more efficient way to compute the followings
> 
> --------------------------------------------------------------------------
> 
> a <- matrix(c(1,1,1,1,2,2,2,2),4,2)
> b <- matrix(c(1,2,3,4),4,1)

Is there a reason why 'b' is a matrix?

Anyway, if b is a matrix with more cols in your real world problem,

sweep(a, 1, b[,1], "/")

will get what you want.

Otherwise

sweep(a, 1, b, "/")

will work whether b is a one col matrix or if you change it to a vector.

HTH

G

> 
> Eventually, I want to get this matrix, `c`.
> 
> c <- matrix(c(1/1,1/2,1/3,1/4,2/1,2/2,2/3,2/4),4,2)
> 
> --------------------------------------------------------------------------
> 
> In fact, #column of `a` is so big..
> 
> Is there a more efficient way to compute this instead of using "apply" or
> something? or "apply" is only way..?
> 
> Any suggestion will be greatly appreciated.
> 
> Regards,
> 
> Kathryn Lord 

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list