How to create a counter by comparing between random number and vector that contains probabilities?

Hey,

I'm a beginner in R :slight_smile:

I created the following dateframe (just an example, the real data frame contains +2000 rows):

RowNum <-c(1:5)
Prob <- c(0.5,0.3,0.8,0.2,0.9)
Attempts <- c(rep(0,5))
Succeeded <- c(rep(0,5))
Overall <- data.frame(RowNum,Prob,Attempts,Succeeded)

I want to create the following:

Creating random number (between 0-1) ->
A. If the random number is lower than the prob in the first row -> add 1 to Attempts and 1 to Succeeded (just for the first row) and then create another random number and move to the next row and check if is greater or lower - if is lower, add 1 to Attempts and 1 to Succeeded (just for the second row) and then create another random number and move to the next row...and so on...
B. If the random number is greater than the prob in the first row -> add 1 only to Attempts (just for the first row) and then create another random number and do the same but don't continue to the next row.

It's like creating a counter for each element in the vector, and to calculation should move to the next row only when the random number is lower than the prob.

Thanks for your help!

I would write a function to do this. There is no need to predefine the Attempts and Succeeded columns but I did it to conform to your example. Also, isn't the Succeeded column always 1?

RowNum <-c(1:5)
Prob <- c(0.5,0.3,0.8,0.2,0.9)
Attempts <- c(rep(0,5))
Succeeded <- c(rep(0,5))
Overall <- data.frame(RowNum,Prob,Attempts,Succeeded)
Overall
#>   RowNum Prob Attempts Succeeded
#> 1      1  0.5        0         0
#> 2      2  0.3        0         0
#> 3      3  0.8        0         0
#> 4      4  0.2        0         0
#> 5      5  0.9        0         0
SuccessFunc <- function(VEC) {
  AttemptResults <- vector(mode = "numeric", length = length(VEC))
  for (i in seq_along(VEC)) {
    Success <- FALSE
    Attempts <- 0
    while (!Success) {
      Candidate <- runif(1, min = 0, max = 1)
      Success <- (Candidate <= VEC[i])
      Attempts <- Attempts + 1
    }
    AttemptResults[i] <- Attempts
  }
  return(AttemptResults)
}

set.seed(123) # Make the example reproducible
Overall$Attempts <- SuccessFunc(Overall$Prob)
Overall$Succeeded <- 1
Overall
#>   RowNum Prob Attempts Succeeded
#> 1      1  0.5        1         1
#> 2      2  0.3        5         1
#> 3      3  0.8        1         1
#> 4      4  0.2        8         1
#> 5      5  0.9        1         1

Created on 2020-11-22 by the reprex package (v0.3.0)

Thanks a lot for your help! Regarding succeeded - you are right, I want to create a for loop that will simulate several iterations. To do so I just need to insert for loop in the beginning of the code?

You can make another for loop or you can make the variable Success a counter and set the while loop to run until Success reaches a given value. That will be more efficient but you will have to store value of Attempts for each value of Success.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.