jeudi 29 octobre 2020

Error: unexpected '}' in " }" for if, else if, else loop in R Studio [closed]

I am trying to bucket random uniform~(0,1) into buckets using an if/else if/else loop but keep receiving the unexpected '}' in " }" error. Below is my code:

u <- runif(100,0,1)
u <- as.matrix(u,nrow=100,ncol=1) #vector of uniforms
x <- as.matrix(rep(0,100),nrow=100,ncol=1)  #vector of zeroes
p1 <- 9/16
p2 <- 15/16
for(i in 1:100){ 
  if (u[i] <= p1) { (x[i] = 0) } #if u[i] is less than p1, assign value of 0
  else if (u[i] > p1 & u[i] <= p2)  {(x[i] = 1) } #if u[i] is in between p1 and p2, assign 
   value of 1
  else (u[i] > p2)  {(x[i] = 2)} #if u[i] is more than p2, assign value of 0
  }

If anyone can help me, I would appreciate it! I would like to be able to print(x) at the end to get a vector of 0, 1, 2s.

Aucun commentaire:

Enregistrer un commentaire