[R] Limit on vector evaluation

Duncan Murdoch murdoch at stats.uwo.ca
Sat Apr 28 03:48:31 CEST 2007


On 4/27/2007 7:13 PM, Robert Barber wrote:
> Dear R Experts,
> 
> Why I try to run this expression:
> 
> x<-sapply(rnorm(rep(10,100000),mean=9,sd=1.5),mean)
> 
> it evaluates the first 10000 values and then stops, but does not return
> to the command prompt.  My cpu keeps running at 100%.  When I exit the
> expression with CTL-C, I then see that x holds 10000 values.  How can I
> evalute the expression 100000 times, or more if I want?

If you interrupt the calculation, then x will be unchanged.  You see 
10000 values there because there were 10000 values the last time you 
finished an assignment to x.

But more importantly, I think you don't understand what your expression 
is calculating.  If you try it with smaller numbers, bit by bit, you may 
be surprised:

 > rnorm(rep(10, 3), mean=9, sd=1.5)
[1] 8.790434 8.444429 8.935716

This doesn't give you 3 sets of 10 values, it gives you one vector of 3 
values.

 > sapply(.Last.value, mean)
[1] 8.790434 8.444429 8.935716

This takes the mean of each entry:  i.e. it does nothing.  I doubt this 
is what you intended to do.

I suspect what you wanted was to generate 100000 sets of 10 values, and 
take the mean of each set.  You can do that this way:

x <- replicate(100000, mean(rnorm(10, mean=9, sd=1.5)))

Duncan Murdoch



More information about the R-help mailing list