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

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Feb 25 15:41:01 CET 2009


hadley wickham wrote:
> And for completeness here's a function that returns the next integer
> on each call.
>
> n <- (function(){
>   i <- 0
>   function() {
>     i <<- i + 1
>     i
>   }
> })()
>   

actually, you do not need the external function to have the functionality:

    n = local({
        i = 0
        function() (i <<- i + 1)[1] })

    n()
    # 1
    n()
    # 2

vQ




More information about the R-help mailing list