better loop for simulation
try: n.questions <- 10 # or however many you want mult.choice <- 2 scores <- rbinom(1000, size = n.questions, prob = 1/mult.choice) On Sat, Jun 18, 2016 at 3:12 PM, Naresh Gurbuxani <
naresh_gurbuxani at hotmail.com> wrote:
I want to calculate a function many times over. My solution below works,
but does not seem very elegant.
# my function to run many times over
stud.score <- function(n.questions, mult.choice = 2) {
prob.success <- 1 / mult.choice
answers <- (runif(n.questions) < prob.success)
return(sum(answers))
}
# my method to run above function 1000 times and store results
count.df <- data.frame(n.count = rep(10, 1000))
scores.df <- apply(count.df, 1, function(x) return(stud.score(x)))
Creating a data frame just to repeat the the count seems wasteful. How
can I generate scores.df without count.df?
Thanks,
Naresh
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Dan Dalthorp, PhD USGS Forest and Rangeland Ecosystem Science Center Forest Sciences Lab, Rm 189 3200 SW Jefferson Way Corvallis, OR 97331 ph: 541-750-0953 ddalthorp at usgs.gov [[alternative HTML version deleted]]