Here you can use the function ifelse
, which does exactly what you want: to return a value if a condition satisfies, and another different if not. For example, for n= 10 and pi=1:
f = ifelse (rnorm(n=10, 0.3, 1) > 1,1,0)
You should easily figure out how to solve your problem with this example.
You could also save your results in a temporal vector, and then check the exact condition you want with boolean operators:
ri = rnorm(n=10, 0.3, 1)
ifelse(-1 < ri & ri < 1,1,0)
Following @M-M comment, you can also obtain values of 1 or 0 just from the evaluated conditional expression. For instance:
as.integer(-1 < ri & ri < 1)