[R] protentially serious R error

Sarah Goslee sarah.goslee at gmail.com
Sat Dec 1 00:06:50 CET 2012


Potentially serious? Only to you: this looks like R FAQ 7.31 striking again.

Here's a hint:


identical(20*(1-0.8), 4)

and another

rnorm(round(20*(1-0.8)))

Sarah

On Fri, Nov 30, 2012 at 4:05 PM, liuxf <liuxf at math.mcmaster.ca> wrote:
> Hi guy,
> I have recently encountered a problem while  I was just trying to generate
> some random numbers with the function "rnorm", the problem is shown below:
> ########case 1############
>> rnorm(20*0.2)
> [1] -1.2765922 -0.5732654 -1.2246126 -0.4734006
> ########case 2###########
> *> rnorm(20*(1-0.8))
> [1] -0.62036668  0.04211587 -0.91092165*
> #########case 3############
>> a<-0.2
>> rnorm(20*a)
> [1]  0.1580288 -0.6545846  1.7672873  0.7167075
> #############case 4#########
> *> b<-1-0.8
>> rnorm(20*b)
> [1] 0.9101742 0.3841854 1.6821761*
>
> I was expecting the 4 cases should do the same job--generate 4 random
> numbers. But in case 2 and 4 I only get 3. Has anyone else seen this problem
> before? Thanks. (I have tried with other functions i.e "rchisq","rexp" ...)
> #######################################################################
> One of my colleague also have a problem that we think it might be related
> with the problem I addressed above:
>
>> test1 <- runif(10,0,1)
>> test1
>  [1] 0.3868379 0.1587814 0.8140483 0.7796691 0.5357628 0.2431110 0.1782747
> 0.3906829 0.5262615 0.7440143
>> test2 <- NULL
>> for(i in seq(0.01,1,length=100)){
> +   test2[i*100] <- sum(test1<i)
> + }
>> test2
>   [1]  0  0  0  0  0  0 *NA*  0  0  0  0  0  0  0  0  1  1  2  2  2  2  2  2
> 2  3  3  3  3  3  3  3  3  3  3  3  3  3  3  4  5  5  5  5  5
>  [45]  5  5  5  5  5  5  5  5  6  7  7  7  7  7  7  7  7  7  7  7  7  7  7
> 7  7  7  7  7  7  7  8  8  8  9  9  9  9 10 10 10 10 10 10 10
>  [89] 10 10 10 10 10 10 10 10 10 10 10 10
>
> Every time he re-runs the code there always always a "NA"(highlighted). Does
> any one know why?  Your help is greatly appreciated.
>
>
> Xiaofeng
>
>


--
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list