There is ambiguity in your question. Do you want different random values for all indexes where a==b
or one random value for all indexes?
The answer by @Rob will work in the second scenario. For the first scenario I suggest avoiding ifelse
:
u<-rep(NA,length(a))
u[a>b] <- 1
u[a