[R] 'for' loop, two variables

2008-07-29 Thread Oehler, Friderike (AGPP)
Dear Rusers, I am still an unexperienced builder of functions and loops, so my question is very basic: Is it possible to introduce a second variable (j) into my loop. To examplify: # This works fine: fn - function (x) {if (x46 x52) 1 else 0} res -NULL for (i in 40:60) res -c(res,fn(i)) res

Re: [R] 'for' loop, two variables

2008-07-29 Thread Duncan Murdoch
On 7/29/2008 7:55 AM, Oehler, Friderike (AGPP) wrote: Dear Rusers, I am still an unexperienced builder of functions and loops, so my question is very basic: Is it possible to introduce a second variable (j) into my loop. To examplify: # This works fine: fn - function (x) {if (x46 x52) 1 else

Re: [R] 'for' loop, two variables

2008-07-29 Thread ONKELINX, Thierry
Onderwerp: [R] 'for' loop, two variables Dear Rusers, I am still an unexperienced builder of functions and loops, so my question is very basic: Is it possible to introduce a second variable (j) into my loop. To examplify: # This works fine: fn - function (x) {if (x46 x52) 1 else 0} res -NULL

Re: [R] 'for' loop, two variables

2008-07-29 Thread Benno Pütz
On 29.Jul.2008, at 14:13, ONKELINX, Thierry wrote: Dear Frederike, #Both your functions are vectorized. So you don't need loops. Working with vectorized functions is much faster than looping. fn - function (x,y) { ifelse(x46 x52 y12, 1, 0) } datagrid - expand.grid(i = 40:60, j = 0:20)