Skip to content
Prev 310562 / 398525 Next

biasing conditional sample

Hello,

The function that follows returns a matrix, not a data.frame but does 
what you ask for.


fun <- function(x, y, n){
     f <- function(x, y){
         while(TRUE){
             rnd <- sample(x, 1)
             if(!any(rnd %in% y)) break
         }
         rnd
     }
     for(i in seq_len(n)){
         tmp <- apply(y, 1, function(.y) f(x, .y))
         y <- cbind(y, tmp)
     }
     y
}


a <- cbind(sample(1:10, 100, TRUE)) # must have dims
fun(1:10, a, 4)  # returns 5 columns, 'a' plus 4


Hope this helps,

Rui Barradas
Em 11-11-2012 19:06, dms at riseup.net escreveu: