Jj Blevins
Jj Blevins

Reputation: 395

Repeat loop with if-condition doesn't work

I want to reorder a vector with 250 values and I am using sample, repeat and if in order to do so:

x <- rnorm(200, mean = 0.06, sd = 0.20)
x$ret_coef = 1 + returns,
X$ret = cumprod(ret_coef) - 1

reorder1 <- function(x){
    repeat{
    temp <- tibble(
      ret= sample(x$ret, 200)
      )

    if(sum(temp$ret[200],temp$ret[180])<0) break
    }
}

Unfortunately, the new vector never fullfills the if-condition.

Upvotes: 0

Views: 134

Answers (1)

Jj Blevins
Jj Blevins

Reputation: 395

I figured it out:

its important to set replace = TRUE:

sample(x$ret, 200, replace=TRUE)

It worked afterwards!

Upvotes: 1

Related Questions