How to randomly select a fixed number of rows without replacement from a data frame in R?

R ProgrammingServer Side ProgrammingProgramming

This can be done simply by using sample function.

Example

> df = data.frame(matrix(rnorm(20), nrow=5))
> df
    X1         X2          X3        X4
1 -0.3277833 -0.1810403  0.2844406 -2.9676440
2 0.8262923   0.4334449  0.4031084 -1.9278049
3 -0.1769219 -0.1583660 -0.2829540 -0.1962654
4 1.0357773   0.9326049  0.3250011 -1.8835882
5 -1.0682642 -0.6589731 -0.4783144 -0.2945062

Let’s say we want to select 3 rows randomly then it can be done as follows −

> df[sample(nrow(df), 3), ]
    X1        X2          X3       X4
2  0.8262923  0.4334449  0.4031084 -1.9278049
1 -0.3277833 -0.1810403  0.2844406 -2.9676440
5 -1.0682642 -0.6589731 -0.4783144 -0.2945062
raja
Published on 06-Jul-2020 17:43:23
Advertisements