Skip to main content

Random number generators, reproducibility and sampling with dplyr


Let's assume that you want to take some random observations from your data set. Dplyr helps you with the function sample_n(). To make your code reproducible you seed the ID of a “random” set of values. You need to indicate number of rows you want to extract and specify if the rows should be replaced or not. To show you how it works I will use again mtcars dataset which is included in your base R program. Let's see first six rows of this data frame. 
library(dplyr)
data("mtcars")
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

sample_n() sample n numbers of random rows 

set.seed(10) 
mtcars%>%sample_n(4,replace = T) # We will take four random rows from mtcars data frame

                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4

sample_frac() is proportional sampling where you need to indicate fraction e.g 0.2 (20%) 

mtcars%>% sample_frac(size=0.2,replace=F) # We will take randomly 20% of mtcars data frame.

                   mpg cyl  disp  hp drat   wt  qsec vs am gear carb
Camaro Z28         13.3   8 350.0 245 3.73 3.84 15.41  0  0    3    4
Cadillac Fleetwood 10.4   8 472.0 205 2.93 5.25 17.98  0  0    3    4
Maserati Bora      15.0   8 301.0 335 3.54 3.57 14.60  0  1    5    8
Merc 280           19.2   6 167.6 123 3.92 3.44 18.30  1  0    4    4
Duster 360         14.3   8 360.0 245 3.21 3.57 15.84  0  0    3    4
Ford Pantera L     15.8   8 351.0 264 4.22 3.17 14.50  0  1    5    4 

In terms of testing for sampling error, in case of big datasets and large sample size, both methods (random and proportional) deliver similar results. Proportional sampling is a better approach for smaller datasets, for smaller sample sizes and if relative group proportions matter. 

Comments