for (i in H) {
if (i == 1)
{Biomp[i]<- Biomp1}
else {Biomp[i] = 0}
}
Hi everyone I am new to Rstudio and I want to create a variable (here I just want to fill the matrix I created) based on a condition.
My code is not working, the outpput is : There were 44 warnings (use warnings() to see them)
You need to iterate over the indices of H, not its values. For this you can use seq_along().
You've supplied a vector to the n parameter of rbinom() but you should ideally pass a scalar (the desired quantity of random numbers). Luckily, rbinom() has a safeguard that if it is supplied a vector with length greater than 1, it uses the length instead. So you didn't get an error but this isn't good practice. Instead use length(individual).
You tried to pre-allocate the matrix you were trying to fill in the for loop (which is good practice) but your code resulted in a matrix with only 1 row. That's because unlike rbinom(), matrix() does not have the safeguard described above. Again, use length(individual) to specify that you want a matrix with 200 rows.
Finally, whenever you're working with random numbers it's a good idea to use set.seed() to make sure that your results are reproducible.
Hi. I just want to add that in a binomial distribution where we want x successes out of n trials with each success having probability p, the r syntax is rbinom(x, size=n, prob=p). So x needs to be less than or equal n. In your rbinom statement, I think you are violating this.
Just a heads-up that you don't need a loop. It's important to remember that R is vectorized. Look at this alternate solution which has no loop but gives same solution.