How to use If with a vector

individual <- c(1:200)
Biomp <- matrix(NA,individual,1)
H <- rbinom(n=individual,size=1,prob=0.3)
Biomp1 <- rbinom(individual, 1, 0.95)

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)

Thanks a lot :grinning:

A few issues with your syntax.

  1. You need to iterate over the indices of H, not its values. For this you can use seq_along().

  2. 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).

  3. 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.

  4. 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)

1 Like

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.

2 Likes

Thank you so much I now anderstand better my mistakes !!!

Thank youuu

Perfect Thank you very much for your help !! :grin:

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.

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
  }
}

Biompalt <- matrix(0, nrow = length(individual)) #initially set everything to 0
idx <- which(H==1)
Biompalt[idx, ] <- Biomp1[idx]
all.equal(Biomp, Biompalt)
#> [1] TRUE

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

1 Like

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