[R] What don't I understand about sample()?

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Thu Mar 13 23:06:48 CET 2025


Yes. If you want repeated calls to sample() you need to call it several times.

Try, for instance replicate(5, sample(1:10)) which is nearly the structure you expected, except transposed.

-pd

> On 13 Mar 2025, at 22.33, Bert Gunter <bgunter.4567 using gmail.com> wrote:
> 
> Bravo for your unrequired R efforts.
> 
> You misunderstand the nested call. sample() is called only once,
> producing 1 sample of 10 with replacement. Since your matrix call
> needs 50 values, ?matrix tells you (in details):
> "If there are too few elements in data to fill the matrix, then the
> elements in data are recycled. If data has length zero, NA of an
> appropriate type is used for atomic vectors (0 for raw vectors) and
> NULL for lists.
> 
> This sort of "recycling" is quite standard in R. Though not universal.
> 
> Cheers,
> Bert
> 
> "An educated person is one who can entertain new ideas, entertain
> others, and entertain herself."
> 
> On Thu, Mar 13, 2025 at 2:23 PM Kevin Zembower via R-help
> <r-help using r-project.org> wrote:
>> 
>> Hello, all,
>> 
>> I'm learning to do randomized distributions in my Stats 101 class*. I
>> thought I could do it with a call to sample() inside a matrix(), like:
>> 
>>> matrix(sample(1:10, replace=TRUE), 5, 10, byrow=TRUE)
>>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>> [1,]    8    2    3    1    8    2    8    8    9     8
>> [2,]    8    2    3    1    8    2    8    8    9     8
>> [3,]    8    2    3    1    8    2    8    8    9     8
>> [4,]    8    2    3    1    8    2    8    8    9     8
>> [5,]    8    2    3    1    8    2    8    8    9     8
>>> 
>> 
>> Imagine my surprise to learn that all the rows were the same
>> permutation. I thought each time sample() was called inside the matrix,
>> it would generate a different permutation.
>> 
>> I modeled this after the bootstrap sample techniques in
>> https://pages.stat.wisc.edu/~larget/stat302/chap3.pdf. I don't
>> understand why it works in bootstrap samples (with replace=TRUE), but
>> not in randomized distributions (with replace=FALSE).
>> 
>> Thanks for any insight you can share with me, and any suggestions for
>> getting rows in a matrix with different permutations.
>> 
>> -Kevin
>> 
>> *No, this isn't a homework problem. We're using Lock5 as the text in
>> class, along with its StatKey web application. I'm just trying to get
>> more out of the class by also solving our problems using R, for which
>> I'm not receiving any class credit.
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business SchoolSolbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list