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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sat 08 Oct 2005 - 06:45:40 EST

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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help