[R] Applying a function to each element of an array

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Oct 7 22:45:40 CEST 2005


On Fri, 7 Oct 2005, Patrick Burns wrote:

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

Well, we don't actually know 'count' is >= 1.  I almost suggested this, 
but suppose the counts were in billions?

It's a good solution for a small set of continguous values.  If there is a 
small set of values, do a two-step lookup using e.g. match to find the 
enumeration of the values.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list