on-internet
← Community

Supply conditional statement as function argument

Stack Overflow ·

I've written a function I use for running Monte Carlo simulations. Within it there is a conditional that stops the simulation based on past and current states. Sensible conditions for stopping the simulation will vary depending on situation, so I'd like to supply these as an argument to the function. I've simplified the function and given a couple of example conditions: simulation1 <- function(states, max.steps=15) { mat <- matrix(NA, nrow=3, ncol=max.steps) mat[, 1] <- 1 for (i in seq_len(max.steps-1)+1) { mat[, i] <- sample(states, 3, rep=TRUE) if (min(mat[, i]) == max(states)) { break } } m

Comments

Sign in or sign up to comment and upvote.

No comments yet.