0

I am trying to generate a matrix sz by first applying a binomial, then adding values from the corresponding column of pombe_new_subs and this combined value being input as size for the following column.

After many frustrations, the following code is what I've ended up with and it just doesn't work - problems I'm coming across are;

# Error in sz[j, i + 1] = sz[, i] + pombe_new_subs[, i] : 
# number of items to replace is not a multiple of replacement length

pombe_new_subs <- rmultinom(3, 15, prob = c(0.3, 0.3, 0.3))

randomdiv <- function(nchrom, ndivs, size) {
    sz <- matrix(nrow = nchrom, ncol = ndivs) 
    for (j in 1:nchrom) {
        n <- size
        for (i in 1:ndivs) {
            n <- rbinom(1, n, 0.5)  
            sz[j,i] <- n
        }
        sz[j,i+1] = sz[ ,i] + pombe_new_subs[ ,i]
        sz[j, i+1] <- n
    }
    return (sz)
}

randomdiv(3, 3, 10)

I know this is probably a fairly simple looping exercise but frustration has entirely taken over.

Jordan
  • 131
  • 8
  • 1
    I think you might want to try indenting your code – Frank Nov 30 '15 at 17:23
  • 1
    You might also consider making your example reproducible. – Heroka Nov 30 '15 at 17:23
  • I think I have indented it @Frank ? I have made a couple of minor adjustments that mean the code should give you a matrix if you type `randomdiv(3, 3, 10)` @Heroka – Jordan Nov 30 '15 at 17:28
  • 1
    @Jordan this might be useful: how to create a [reproducible example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Your code should be able to run and yield your described error in a new session, without having to make assumptions about variables or how the function is called. – Heroka Nov 30 '15 at 17:35
  • @Heroka I think this should work and should generate the error I'm getting – Jordan Nov 30 '15 at 21:24
  • 1
    @Frank thank you for demonstrating what you meant by indenting, I will remember in future! – Jordan Nov 30 '15 at 21:26

0 Answers0