[Rd] sample (PR#2546)

Patrick Burns pburns at pburns.seanet.com
Thu Mar 6 08:49:47 MET 2003


First off, the fix that I propose doesn't change the behaviour when the 
argument is
numeric.  We are certainly stuck with sample being "helpful" in that case.

The proposed fix does allow a reasonable workaround when the first argument
might have length one:

as.numeric(sample(x + 0+0i, ...))

Regards,

Pat


kjetil brinchmann halvorsen wrote:

>On 25 Feb 2003 at 19:05, Patrick Burns wrote:
>
>  
>
>>There is rumour that some of my mail didn't go where it should have because
>>I don't know how to work email.  It was suggested that I summarize here.
>>
>>The issue is with sample when the first argument is length 1.  Current
>>behaviour is:
>>
>>    
>>
>
>I thought about this. It would be nice to have the behaviour Patrick 
>Burns proposes, but there is a problem: What to do when the 
>length one argument is numeric? Look at the following:
>
>  
>
>>sample(1)
>>    
>>
>[1] 1
>  
>
>>sample(7)
>>    
>>
>[1] 4 6 1 5 2 3 7
>  
>
>>sample(6.9)
>>    
>>
>[1] 2 4 5 1 3 6   # 6.9 truncated to 6
>  
>
>>sample(0)     
>>    
>>
>[1] 0                #  What one should expect here is not easy to 
>                      #  know, as the help page requires a positive
>                      #  integer
>  
>
>>sample(-3)
>>    
>>
>[1] -3              # same
>  
>
>>sample(0.5)
>>    
>>
>[1] 0.5
>  
>
>>sample(1.5)
>>    
>>
>[1] 1               # truncated
>
>So what to do with length one numerical arguments, truncate, as seems 
>to be done (but not always):
>
>  
>
>>sample(0.99)
>>    
>>
>[1] 0.99
>
>or round, or give an error if not a positive integer, or? 
>However it is done it will surely break some code. 
>
>Kjetil Halvorsen
>
>
>  
>
>> > sample('a', 1)
>>Error in sample(x, size, replace, prob) : invalid first argument
>> > sample(3+0i, 1)
>>Error in x >= 1 : illegal comparison with complex values
>> > sample(TRUE, 1)
>>[1] 1
>> > sample(FALSE, 1)
>>[1] FALSE
>> > sample(NA, 1)
>>Error in if (length(x) == 1 && x >= 1) { :
>>        missing value where logical needed
>> > sample(.5, 1)
>>[1] 0.5
>>
>>
>>The proposed behaviour is:
>>
>> > sample('a', 1)
>>[1] "a"
>> > sample(3+0i, 1)
>>[1] 3+0i
>> > sample(TRUE, 1)
>>[1] TRUE
>> > sample(FALSE, 1)
>>[1] FALSE
>> > sample(NA, 1)
>>[1] NA
>> > sample(.5, 1)
>>[1] 0.5
>>
>>
>>For some reason that I haven't yet grasped, the current
>>behaviour seems to be preferred to the proposed behaviour.
>>
>>Pat Burns
>>
>>patrick at burns-stat.com
>>
>>______________________________________________
>>R-devel at stat.math.ethz.ch mailing list
>>http://www.stat.math.ethz.ch/mailman/listinfo/r-devel
>>    
>>
>
>
>
>
>
>  
>



More information about the R-devel mailing list