A few issues with your syntax.
-
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.
set.seed(42)
individual <- c(1:200)
H <- rbinom(n = length(individual), size = 1, prob = 0.3)
Biomp1 <- rbinom(individual, 1, 0.95)
Biomp <- matrix(nrow = length(individual))
for (i in seq_along(H)) {
if (H[i] == 1) {
Biomp[i, ] <- Biomp1[i]
} else {
Biomp[i, ] <- 0
}
}
head(Biomp)
#> [,1]
#> [1,] 1
#> [2,] 1
#> [3,] 0
#> [4,] 1
#> [5,] 0
#> [6,] 0
Created on 2020-07-22 by the reprex package (v0.3.0)