How do I select a sample of rows at random with repetition from a matrix in R?
So do be clear, I would start with a matrix of, for example, 100 rows and I would be able to select 5 of those rows and make a new matrix. I would want the option of doing this either with or without replacement.
Use sample
on the rows with replace=TRUE
or replace=FALSE
.
If X
is your original matrix then
X[sample(nrow(X),size=5,replace=TRUE),]
or
X[sample(nrow(X),size=5,replace=FALSE),]
should work. (It may be more readable if you choose the sample first: s <- sample(...)
and then subset: newmat <- X[s,]
)
use the sample
function:
x <- matrix(1:1000, nrow=100)
With replacement:
x[sample(1:100, 5, replace=TRUE), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 19 119 219 319 419 519 619 719 819 919
[2,] 51 151 251 351 451 551 651 751 851 951
[3,] 42 142 242 342 442 542 642 742 842 942
[4,] 48 148 248 348 448 548 648 748 848 948
[5,] 73 173 273 373 473 573 673 773 873 973
Wihtout replacement:
x[sample(1:100, 5, replace=FALSE), ]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 64 164 264 364 464 564 664 764 864 964
[2,] 67 167 267 367 467 567 667 767 867 967
[3,] 20 120 220 320 420 520 620 720 820 920
[4,] 17 117 217 317 417 517 617 717 817 917
[5,] 6 106 206 306 406 506 606 706 806 906
This seems to work better with data frames:
sample_df<-x[sample.int(nrow(x),size=100,replace=TRUE),]