Skip to content
Prev 78580 / 398502 Next

Applying a function to each element of an array

If there weren't an analytic solution to your problem,
then you could build a vector of the answers from 1
to the maximum in the matrix.  Call that 'wtvec'.  Then:

ans <- array(NA, dim(A), dimnames(A))
ans[] <- wtvec[as.vector(A)]

should get you what you want.


Patrick Burns
patrick at burns-stat.com
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")
Tim Smith wrote: