Hi Again, I am writing a 'for loop' to create a matrix of randomly sampled colors. I've written this loop in matlab and it works fine. I then tried to do it in R and apparently there is something wrong with my syntax b/c every time I run the script, the for loop "blows up" at a different point in the loop.
As you can see, I ask R to clear my workspace each time, so there shouldn't be any variables in my workspace that are messing this up. rm(list=ls()) # Sample Size size <- 53 # Probability of each color based on company P.company <- c(.14,.14,.16,.13,.20,.24) # Names of colors color <- c('Br','Y','G','R','O','Bl') # Make an empty matrix that will be filled in by for loop table.combos <- matrix(nrow = 10000, ncol = 6); # For loop will run through choosing a random bag of 53 M&Ms 10000 times # and create a table enumerating the number of each color in each of these 10000 bags for(i in 1:10000) { combos <- sample(color,size, replace = TRUE, prob = P.company) table.combos[i, ] <- table(combos) colnames(table.combos)<-c("Br","Y","G","R","O","Bl") } Every time the loop blows up, I get back this error: Error in table.combos[i, ] <- table(combos) : number of items to replace is not a multiple of replacement length There is no apparent consistency on which "i" in the loop I get this error. Sometimes i = 10, sometimes i = 685, sometimes i = 1954, sometimes i = 59, etc. If anyone can please tell me what I am doing wrong, it would be greatly appreciated! Cheers, Melissa -- View this message in context: http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4336585.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list 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.