[R] Too slow to execute!

rolf at math.unb.ca rolf at math.unb.ca
Sun Apr 29 18:44:16 CEST 2007


Chuck Cleland wrote:

        <snip>

> Why use a loop here at all?  It seems like this would do it pretty fast:
> 
> Re <- rnorm(n = 2e+06, mean = - 1)
> 
> > system.time(Re <- rnorm(n = 2e+06, mean = - 1))
> [1] 0.77 0.01 0.78   NA   NA
> 
>   I'm not sure what you expect Re to be (a list with 100000 vectors of
> length 20?), but you could reshape one long vector into a matrix or
> whatever you might need.  For example:
> 
> Re.mat <- matrix(Re, ncol=20)

        <snip>

        Ummmmmm, not that it really matters, but doesn't the
        original construction (x-2*10)/20, give variates with
        mean = -1 and standard deviation = 1/20 = 0.05?

        (One wonders why ``(x-2*10)/20'' rather than ``(x-20)/20'',
        even if one allows for doing the operation in this
        convoluted way.)

        It might be added that ``Re'' is not a good name for
        an object since it is the name of a function (which
        returns the real part of complex numbers).

                                cheers,

                                        Rolf Turner
                                        rolf at math.unb.ca



More information about the R-help mailing list