I have a say function,
inc <- function(t) { f <- 1 t + f }
So, the first time the function is called inc, it fwill 1, but the next time it gets the name f, the value should be 2and when the function incgets the call for the third value it fshould be 3and so on ...
inc
f
1
2
3
How do I do this in R?
I usually use this. I don't know if this is a trick or hack:
getF <- function(){ x <- 1 function(t){ x <<- t + x } } f <- getF()
f - ( getF), , , f . environment(f). <<- x : . ls(environment(f)) get("x", environment(f)).
getF
environment(f)
<<-
x
ls(environment(f))
get("x", environment(f))
print(f(3))#4 print(f(4))#8
, :
inc <- function(t) { if (!exists("e")) { e <<- new.env() e$x <- 1 } e$x <- e$x + t e$x } inc(2) #[1] 3 inc(2) #[1] 5
Source: https://habr.com/ru/post/1608273/More articles:Find the minimum cosine distance between two matrices - vectorizationВывод аргумента шаблона строкового литерала - c++JAGS - hierarchical model comparison, not jumping between models even with pseudo-welds - winbugshorizontal recyclerView with wrap_content in vertical recyclerView - androidGoogle Plus Signs Of Crashing In iOS 9 - iOSIs there a way to pass a PHP object as a JavaScript parameter in HTML? - javascriptto get the total number of hours from two hours - javaHow to convert string to boolean state in java / android? - javaTop PostgreSQL function for ascii 152 character ("ÿ") - sqlProblem trying to connect to cluster after upgrading Java SDK version - couchbaseAll Articles