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.
Hello,> > 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 >This is because not all colors are present in that one sample that breaks the code.> > There is no apparent consistency on which "i" in the loop I get this > error. >There shouldn't be, every time you run the loop, 'sample' will take different values. To get reproducibility you would need 'set.seed'. A solution could be based on the following. It's important to sort, like the difference between 'tc1' and 'tc2' proves. It's 'tc2' you want. Note that I've reduced the sample size to 10. Note also that I initialize the matrices to zeros, not NA's. color <- sort(c('Br','Y','G','R','O','Bl')) tc1 <- matrix(0, nrow = 1, ncol = 6) tc2 <- matrix(0, nrow = 1, ncol = 6) colnames(tc1) <- color colnames(tc2) <- color set.seed(123) for(i in 1) { combos <- sample(color, 10, replace = TRUE, prob = P.company) tc1[i, unique(combos)] <- table(combos) tc2[i, sort(unique(combos))] <- table(combos) } table(combos) tc1 tc2 I hope this helps. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/For-Loop-Error-tp4336585p4337469.html Sent from the R help mailing list archive at Nabble.com.
On Sat, Jan 28, 2012 at 10:25:47AM -0800, Melrose2012 wrote:> 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 lengthHi. Rui Barradas already pointed out that the problem is with samples, which do not contain all colors. Forcing table() to produce zero frequencies of colors, which do not occur, a factor may be used. Try, for example size <- 53 P.company <- c(.14,.14,.16,.13,.20,.24) color <- c('Br','Y','G','R','O','Bl') table.combos <- matrix(nrow = 10000, ncol = 6); colnames(table.combos) <- color for(i in 1:10000) { combos <- sample(color,size, replace = TRUE, prob = P.company) table.combos[i, ] <- table(factor(combos, levels=color)) } which(table.combos==0, arr.ind=TRUE) row col [1,] 588 1 [2,] 3005 1 [3,] 4535 1 [4,] 8314 1 [5,] 7654 2 [6,] 8607 3 [7,] 1790 4 [8,] 1980 4 [9,] 2991 4 [10,] 4550 4 [11,] 5868 4 [12,] 6704 4 [13,] 8769 4 [14,] 8823 4 [15,] 7861 5 Hope this helps. Petr Savicky.