Select Random Samples in R using Dplyr – (sample_n() and sample_frac())

Sample_n() and Sample_frac () are the functions used to select random samples in R using Dplyr Package.  Dplyr package in R is provided with sample_n() function which selects random n rows from a data frame.  Sample_frac () in R returns the random N% of rows.

We will be using mtcars data to depict the above functions

 

sample_n() Function in Dplyr  

The sample_n function selects random rows from a data frame (or table). First parameter contains the data frame name, the second parameter of the function tells R the number of rows to select.

library(dplyr)
mydata <- mtcars

# select random 4 rows of the dataframe 
sample_n(mydata,4)

In the above code sample_n() function selects random 4 rows of the mtcars dataset. so the result will be

Select Random Samples in R with Dplyr sample_frac() Function

 

sample_frac() Function in Dplyr :

The sample_frac() function selects random n percentage of rows from a data frame (or table). First parameter contains the data frame name, the second parameter tells what percentage of rows to select

library(dplyr)

mydata <- mtcars

# select random 20 percentage rows of the dataframe 
sample_frac(mydata,0.2)

In the above code sample_frac() function selects random 20 percentage of rows from mtcars dataset. So the result will be.

Select Random Samples in R with Dplyr sample_frac() Function 1

Select Random Samples in R with Dplyr – (sample_n() and sample_frac()) next