Re: [R] Alternate to for-loop

2009-02-17 Thread Wacek Kusnierczyk
Stefan Evert wrote: > >> hmm, would you be saying that r's vectorised performance is overhyped? >> or is it just that non-vectorised code in r is slow? > > What I meant, I guess, was (apart from a little bit of trolling) that > I'd had misconceptions about the speed differences between loops and >

Re: [R] Alternate to for-loop

2009-02-17 Thread Stefan Evert
I ran into a similar issue with a simple benchmark the other day, where a plain loop in Lua was faster than vectorised code in R ... hmm, would you be saying that r's vectorised performance is overhyped? or is it just that non-vectorised code in r is slow? What I meant, I guess, was (apar

Re: [R] Alternate to for-loop

2009-02-17 Thread Wacek Kusnierczyk
Stefan Evert wrote: > A couple of remarks on vQ's naive benchmark: > >> f.rep = function(n, m) replicate(n, rnorm(m)) > > I suppose you meant > > f.rep = function(n, m) replicate(n, mean(rnorm(m))) > > which doesn't make a substantial speed difference, though. indeed, thanks; i've already pos

Re: [R] Alternate to for-loop

2009-02-16 Thread Wacek Kusnierczyk
Patrick Burns wrote: > Wacek Kusnierczyk wrote: >> Patrick Burns wrote: >> >>> If the goal is to "look" professional, then >>> 'replicate' probably suits. If the goal is to >>> compute as fast as possible, then that isn't >>> the case because 'replicate' is really a 'for' >>> loop in disguise an

Re: [R] Alternate to for-loop

2009-02-16 Thread Stefan Evert
A couple of remarks on vQ's naive benchmark: f.rep = function(n, m) replicate(n, rnorm(m)) I suppose you meant f.rep = function(n, m) replicate(n, mean(rnorm(m))) which doesn't make a substantial speed difference, though. f.pat = function(n, m) colMeans(array(rnorm(n*m), c(n, m)))

Re: [R] Alternate to for-loop

2009-02-16 Thread Patrick Burns
Wacek Kusnierczyk wrote: Patrick Burns wrote: If the goal is to "look" professional, then 'replicate' probably suits. If the goal is to compute as fast as possible, then that isn't the case because 'replicate' is really a 'for' loop in disguise and there are other ways. Here's one other way

Re: [R] Alternate to for-loop

2009-02-16 Thread Wacek Kusnierczyk
Patrick Burns wrote: > If the goal is to "look" professional, then > 'replicate' probably suits. If the goal is to > compute as fast as possible, then that isn't > the case because 'replicate' is really a 'for' > loop in disguise and there are other ways. > > Here's one other way: > > function (si

Re: [R] Alternate to for-loop

2009-02-16 Thread Gustaf Rydevik
On Mon, Feb 16, 2009 at 12:59 PM, megh wrote: > > Hi, I am trying to create a vector of length 10 (say), wherein each element > will be average of random sample of size 100, from a distribution, say > Normal. Can anyone please tell me without creating a "for" loop, how I can > do that? > > Regards

Re: [R] Alternate to for-loop

2009-02-16 Thread Patrick Burns
If the goal is to "look" professional, then 'replicate' probably suits. If the goal is to compute as fast as possible, then that isn't the case because 'replicate' is really a 'for' loop in disguise and there are other ways. Here's one other way: function (size, replicates, distfun, ...) {

Re: [R] Alternate to for-loop

2009-02-16 Thread Uwe Ligges
megh wrote: No, it is not homework. I obviously For some value of "obvious" as you has not given a single line of code as the posting guide suggests. You probably want: replicate(10, mean(rnorm(100))) Uwe Ligges could do that using a for-loop, and that I already did. However I though

Re: [R] Alternate to for-loop

2009-02-16 Thread megh
No, it is not homework. I obviously could do that using a for-loop, and that I already did. However I thought whether there could be a better approach as it was looking very messy and unprofessional. Uwe Ligges-3 wrote: > > > > megh wrote: >> Hi, I am trying to create a vector of length 10 (

Re: [R] Alternate to for-loop

2009-02-16 Thread Uwe Ligges
megh wrote: Hi, I am trying to create a vector of length 10 (say), wherein each element will be average of random sample of size 100, from a distribution, say Normal. Can anyone please tell me without creating a "for" loop, how I can do that? Homework? Then please ask you course material or

Re: [R] Alternate to for-loop

2009-02-16 Thread Henrique Dallazuanna
Try this: replicate(10, mean(rnorm(100))) On Mon, Feb 16, 2009 at 8:59 AM, megh wrote: > > Hi, I am trying to create a vector of length 10 (say), wherein each element > will be average of random sample of size 100, from a distribution, say > Normal. Can anyone please tell me without creating a