DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • How Trustworthy Is Big Data?

Trending

  • Driving DevOps With Smart, Scalable Testing
  • Memory Leak Due to Time-Taking finalize() Method
  • System Coexistence: Bridging Legacy and Modern Architecture
  • Proactive Security in Distributed Systems: A Developer’s Approach
  1. DZone
  2. Data Engineering
  3. Big Data
  4. Data Sampling Methods in R

Data Sampling Methods in R

Read this article in order to learn more about data sampling methods in R and what two types sampling methods can be broadly classified in.

By 
Sibanjan Das user avatar
Sibanjan Das
DZone Core CORE ·
Jun. 06, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
39.1K Views

Join the DZone community and get the full member experience.

Join For Free

We are living in a Big Data world. A massive amount of data is generated every day, and we are trying to crunch the data to derive useful information out of it. It is one of the reasons that fueled the growth of Advanced Analytics or Data Science. The machine learning and statistical methods are highly benefitted when we supply them with the right volume of data. However, we can develop good models even with reasonable datasets. The trick here is a proper data sampling technique.

Sampling is nothing more than a subset of data. Sampling methods can be broadly classified into two types:

1. Probability Sampling: In this technique, the samples are chosen at random, and each sample has a known probability of being selected. There are different kinds of probability sampling techniques and will be discussed in this article.

2. Non-probability Sampling: In this technique, the odds of observations being selected is not defined and selection is carried out by the subjective judgment of the researchers.

Probability Sampling using R

Simple Random Sampling

A simple random sample is generated by a design, which warrants that each subgroup of the population of size n has an equal probability of being picked as the sample. In R, we can draw a random sample of size 10 from the numbers 1 to 1000, using the sample function sample (1:1000, 10) . If we try this multiple times, we might get different results. This is correct behavior, however, sometimes we might need the results to be reproducible. In that case, we need to set the seed value. If we set the seed value, the random number sequence is set to a known value. In R, the random number generator is not truly random but pseudo-random as it is generated by an algorithm that returns the same pseudo-random sequence whenever we start seed for the process. We can use the set.seed () function to specify the seed value. The value can be any numeric value.

Another important argument in the sample() is the replace parameter. By default, it is set to FALSE. This means the first number picked for sampling will not be picked up again in the sample. When we set it to TRUE, the sample elements might repeat in the sample. This is also known as sampling with replacement.

Sometimes, we might need to use sample function to simulate a poll or survey. For example, we plan to design an experiment for a survey where we plan to know whether people like our products. The two responses that we are allowed are "Yes" or "No". Before sending out the surveys to 500 people, we prepare a demo analysis for which there is a need to generate a sample with a response ratio of 9:1. To simulate this we can do the following:

sample(1:20000, 500, replace = TRUE, prob = c(0.90, 0.10))

Stratified Sampling

In stratified sampling, the data population is divided into groups, and samples are taken from each group. The partitioning of the population into groups is called strate, and simple random sample for each group is called stratum.

In R, we can generate stratified sampling using various methods. One of the ways is shown below:

library(dplyr)
set.seed(1)
sample_iris <- iris %>%
  group_by(Species) %>%
  sample_n(10)

There are other sampling methods, like cluster sampling, systematic sampling and multistage sampling which are not widely used in the industries. In R, you can explore about this from the package survey.

R (programming language) Big data

Opinions expressed by DZone contributors are their own.

Related

  • Customer 360: Fraud Detection in Fintech With PySpark and ML
  • Mastering Advanced Aggregations in Spark SQL
  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • How Trustworthy Is Big Data?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!