Skip to content
Prev 171694 / 398513 Next

Have a function like the "_n_" in R ? (Automatic count function )

hadley wickham wrote:
not as it stands above, because you immediately apply your function and
lose grip of it -- so it's just as do-once a solution as that with
local.  but clearly, to have multiple independent counters, you'd need
two nested functions, as in this generalized version:

    make.counter =
        function(value)
            function(increment)
                (value <<- value + increment)[1]

    counters =
        lapply(rep(0, 3), make.counter)
    mapply(
        function(counter, increment)
            counter(increment),
        counters, 1)
    # 1 1 1

which is what you presumably had in mind, roughly.

vQ