SpelaB
SpelaB

Reputation: 23

R improvement of time consuming repeat function

I need to generate random sample (shuffling only values within column), check if it meets condition and store the "good" ones. I need 1000 random samples. With help of other posts I made this code, but it is awfully time consuming. Is there a better solution?

ds = matrix(sample(0:1000, 120), ncol=20)

rep <- function(ds) {
    success <- FALSE
  while (!success) {
    x <- apply(ds,2,sample, replace=TRUE)
    success <- all(as.logical(colSums(x) <=  colSums(ds)))
  }
  #compute something based on random matrix that meets condition and return 
  #value
  }
  y=mean(x)
  return(y)
}
replicate(1000, {rep(ds)})

Thank you!

Upvotes: 2

Views: 85

Answers (1)

amatsuo_net
amatsuo_net

Reputation: 2448

Here is the idea I wrote in the comment suc_samp return a successful sampling for a vector and my_rep apply this successful sampling to each column (rep is a base R function so you may want to avoid masking it).

suc_samp <- function(x) {
  while(1) {
    x_samp <- sample(x, size = length(x), TRUE)
    if(sum(x_samp) <= sum(x)) break
  }
  return(x_samp)
}

my_rep <- function(ds) {
  x <- apply(ds, 2, suc_samp)
  y <- mean(x)
  return(y)
}

ds <- matrix(sample(0:1000, 120), ncol=20)

replicate(1000, {my_rep(ds)})

Upvotes: 2

Related Questions