How to use IF function within APPLY function in R?
<p>These are commands showing the position of a man who walks randomly in four directions (Using the FOR LOOP is not allowed):  </p>

<pre><code>mat=matrix(c(1,0,0,1,-1,0,0,-1,0,0),ncol=2,byrow=T)
plot(apply(replicate(100,mat[sample(1:5,1),]),1,cumsum),type='l')
</code></pre>

<p>I'd like to limit the plot box for example between (-5,5) in both X and Y axis, so that when the man arrive at this limitations, he should continue walking without passing the limitation (I mean, continue in other three directions). But I don't know where to place the IF condition! </p>