Re: [R] Simulation numbers from a probability table

2010-01-13 Thread Peter Ehlers
Try this: dat <- data.frame(x=11:14, pa=1:4/10, pb=4:1/10) f <- function(numreps, data){ pmat <- as.matrix(data[-1]) x <- data[,1] result <- matrix(0, nrow=numreps, ncol=ncol(pmat)) colnames(result) <- c("A", "B") for(i in seq_len(numreps)){ result[i,] <- apply(pmat, 2, function(p)

Re: [R] Simulation numbers from a probability table

2010-01-13 Thread Tal Galili
If the trials are not connected then I would consider melting the table using melt() from the reshape package. And then using lapply() with the function random.function <- function(my.prob, number.of.observations = 10) { sum(rbinom(number.of.observations, 1, my.prob)) } in case the trials are con

[R] Simulation numbers from a probability table

2010-01-13 Thread Kelvin
Dear friends, If I have a table like this, first row A B C D ... are different levels of the variable, first column 0 1 2 4 ... are the levels of the "numbers", the numbers inside the table are the probabilities of the "number" occuring. A B C D... 0 0.20.30.1