[R] r-ish ? how can i improve my code?

2003-10-15 Thread Sean O'Riordain
Hi Folks, I'm trying to learn R. One of my intentions is to do some Monte-Carlo type modelling of road accidents. Below, to simplify things, I've appended a little program which does a 'monte-carlo' type simulation. However, it is written in a way which seems a bit un-natural in R. Could

Re: [R] r-ish ? how can i improve my code?

2003-10-15 Thread Spencer Graves
1. I suggest you avoid using c as a loop index, as it conflicts with the name of a function. R is smart enough to figure out the difference in some cases but not in all. 2. How about the following: n - 900 x - runif(n) y - runif(n) z - sqrt(x^2+y^2) print(list(Overwork.ratio =

Re: [R] r-ish ? how can i improve my code?

2003-10-15 Thread Patrick Burns
For this particular problem you can probably use polar coordinates. But something similar to your code could be: x - runif(900) y - runif(900) z - sqrt(x^2 + y^2) okay - z 1 while(any(!okay)) { n.bad - sum(!okay) x[!okay] - runif(n.bad) y[!okay] - runif(n.bad) z - sqrt(x^2 + y^2) #

Re: [R] r-ish ? how can i improve my code?

2003-10-15 Thread Philipp Pagel
On Wed, Oct 15, 2003 at 10:06:36AM +0100, Sean O'Riordain wrote: n - 900; # number of valid items required... x - numeric(n); y - numeric(n); z - numeric(n); c - 1; # current 'array' pointer tc - 0; # total items actually looked at... while (c = n) { x[c] = runif(1, 0, 1);

Re: [R] r-ish ? how can i improve my code?

2003-10-15 Thread Sean O'Riordain
Hi Patrick, Spencer, Thanks for that! Both your solutions are MUCH quicker! afaik overwork_ratio = 4/pi # what a hard way to calculate this :-) The real problem I'm actually working on is a little more complicated :-) - 6 'random' variables, and 18 dependant variables (at last count)...