You have understood what is sampling theory in the previous post. Let us understand how to perform various types of sampling in R.
To perform sampling in R, one can take the help of various functions available for each type of sampling technique.

Simple Random Sampling in R
Simple random sampling can be done in R using the function ‘sample()’. It accepts an argument called replace to decide whether it has to be with replacement or without replacement of values in the original set.
replace=TRUE mean repetitions of values are allowed.
replace=FALSE mean repetitions of values are NOT allowed.
Let us look at the below example. From the first 100 numbers, if you want to select 12 numbers randomly without repetition.
Here, the function parameters are explained below.
- x: Range of values from where random selection has to be performed
- size: number of values to
select replace : whether to allow repetition or not.
1 2 3 4 5 6 |
# Simple Random Sampling Without Replacement # Defining range of values from 1 to 100 SampleRange=c(1:100) # Choosing a sample of size 12 without repetition sample(x=SampleRange,size=12, replace=FALSE) |
Output:

1 2 3 4 5 6 |
# Simple Random Sampling With Replacement # Defining range of values from 1 to 100 SampleRange=c(1:100) # Choosing a sample of size 12 with repetition sample(x=SampleRange,size=12, replace=TRUE) |
Output:

Stratified Sampling in R
As discussed, stratified sampling needs a grouping reference. Based on this column data rows can be selected from each group.
There are many libraries in R to perform stratified sampling, two of the easiest to use are listed below
- library(sampling) —> strata() function
- library(caret) —> createDataPartition() function
Lets use below tiny data to understand this concept
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Defining Sample Data for Stratified Sampling GroupName=c('A','A','B','B','B','B','C','C','C') Value=c(2,1,50,55,57,58,201,205,207) SampleData=data.frame(GroupName,Value) print(SampleData) # Performing Stratified Sampling using library caret library(caret) SampleIndex=createDataPartition(SampleData$GroupName, p= 0.5, list=FALSE) # SampleIndex will have rows selected from each group print(SampleIndex) # Performing Stratified Sampling using library sampling library(sampling) SampleIndexNew=strata(SampleData, c('GroupName'), size=c(1, 2, 3), method="srswor") # SampleIndexNew will have mentioned rows selected from each group print(SampleIndexNew) |
Output:

Output(SampleIndex):

Output(SampleIndexNew):

Systematic Sampling in R
seq() function in R helps to generate the ith index
Systematic Sampling means just select every ith value from the dataset.
1 2 |
# Generate every 5th index between range 0 to 50 seq(from = 0, to = 50, by =5) |
Output:

Biased Sampling in R
Biased sampling does not require any special function in R. One can select any index of value as per the need and inspect it.
1 2 3 |
# Selecting first five index for inspection selectedIndex=c(1:5) print(selectedIndex) |
Output:

Conclusion
- Sampling means choosing random values.
- A randomly selected sample is representative of the whole group (population).
- Simple Random Sampling in R is done using the sample() function
- Systematic Sampling in R is done by using the seq() function.
- Biased Sampling in R is done by choosing the sample indexes manually.