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.
This seems to work better with data frames:
use the
sample
function:With replacement:
Wihtout replacement:
Use
sample
on the rows withreplace=TRUE
orreplace=FALSE
.If
X
is your original matrix thenor
should work. (It may be more readable if you choose the sample first:
s <- sample(...)
and then subset:newmat <- X[s,]
)