18 C
London
Thursday, July 24, 2025
HomeStatistics TutorialRCluster Sampling in R (With Examples)

Cluster Sampling in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

Researchers often take samples from a population and use the data from the sample to draw conclusions about the population as a whole.

One commonly used sampling method is cluster sampling, in which a population is split into clusters and all members of some clusters are chosen to be included in the sample.

This tutorial explains how to perform cluster sampling in R.

Example: Cluster Sampling in R

Suppose a company that gives city tours wants to survey its customers. Out of ten tours they give one day, they randomly select four tours and ask every customer to rate their experience on a scale of 1 to 10.

The following code shows how to create a fake data frame in R to work with:

#make this example reproducible
set.seed(1)

#create data frame
df #view first six rows of data frame
head(df)

  tour experience
1    1   6.373546
2    1   7.183643
3    1   6.164371
4    1   8.595281
5    1   7.329508
6    1   6.179532

And the following code shows how obtain a sample of customers by randomly selecting four tours and including every member in those tours in the sample:

#randomly choose 4 tour groups out of the 10
clusters unique(df$tour), size=4, replace=F)

#define sample as all members who belong to one of the 4 tour groups
cluster_sample %in% clusters, ]

#view how many customers came from each tour
table(cluster_sample$tour)

 2  7  8 10 
20 20 20 20 

From the output we can see that:

  • 20 customers from tour group #2 were included in the sample.
  • 20 customers from tour group #7 were included in the sample.
  • 20 customers from tour group #8 were included in the sample.
  • 20 customers from tour group #10 were included in the sample.

Thus, this sample is composed of 80 total customers that came from 4 different tour groups.

Related: How to Use %in% Operator in R

Additional Resources

Understanding Different Types of Sampling Methods
Stratified Sampling in R
Systematic Sampling in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories