Kevin, It is simple. Your matrix has fifty entries and you supplied just 10. R tends to quietly assume you want the sample repeated as often as needed as long as it can be used in whole amounts. So, you get five copies. If you interchanged rows and columns with byrow=FALSE then every two rows would repeat.
Ask for 50! matrix(sample(1:50, replace=TRUE), 5, 10, byrow=TRUE) But decide what you want. You are getting numbers in the range of 10. Asking for 50 as I showed will get you something like this: matrix(sample(1:50, replace=TRUE), 5, 10, byrow=TRUE) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 15 32 20 4 44 20 34 2 30 14 [2,] 20 8 42 8 46 45 10 27 27 9 [3,] 26 12 15 26 8 47 25 31 38 31 [4,] 47 5 2 28 13 33 19 3 3 49 [5,] 12 1 11 3 12 21 1 19 30 31 What you may want is this with size=5*10 matrix(sample(x=1:10, size=5*10, replace=TRUE), 5, 10, byrow=TRUE) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 2 1 9 9 3 5 7 4 4 10 [2,] 4 1 8 7 1 1 5 1 6 10 [3,] 4 3 6 2 4 4 10 10 8 8 [4,] 10 6 3 2 8 10 10 2 7 9 [5,] 2 4 2 5 5 10 10 10 8 1 -----Original Message----- From: R-help <r-help-boun...@r-project.org> On Behalf Of Kevin Zembower via R-help Sent: Thursday, March 13, 2025 5:00 PM To: r-help@r-project.org Subject: [R] What don't I understand about sample()? 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@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@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.