I run a simulation, I need to track the number of occurrences in a function call of a certain condition. I tried to accomplish this with an assignment to a global object. It works if you run the function, but if you try to execute the lapply function, as I do, you will get a single count of all the cases when this condition happened, and not a count for every time this happened for each item in the list fed to lapply .
Here is a fictitious situation where the occurrence is the uniformity of the number:
FUN <- function(x){ lapply(1:length(x), function(i) { y <- x[i] if (y %% 2 == 0){ assign("count.occurrences", count.occurrences + 1, env=.GlobalEnv) } print("do something") }) list(guy="x", count=count.occurrences) }
This is a simulation, so speed is a problem. I want it to be as fast as possible, so I am not married to the idea of a global destination.
source share