What does sample function do in R

This recipe explains what does sample function do in R

Recipe Objective

In R, we use sample() function whenever to want to generate a random sample of a specified from dataset. This can be done with or without replacement. We can create a numeric or character vector sample using sample() function. ​

Whenever you are generating random sample, you are using an algorithm that requires a seed whose function is to initialise. These numbers are actually pseudorandom numbers which can be predicted if we know the seed and the generator. ​

Setting a seed means iniltialising a pseudorandom generator. We set a seed when we need the same output of numbers everytime you want to generate random numbers. If we don't set a seed, the generated pseudorandom numbers are different on each execution. ​

Scaling Data with FEAST Feature Store for Machine Learning

In most of the simulation methods in statistics, random numbers are used to mimic the properties of uniform or normal distribution in a certain interval. ​

In this recipe, you will learn how to use sample() function by setting a seed. ​

Example:

Generating a sample of 10 random numbers between 1 and 30 by setting a seed without replacement (i.e. every value will be unique) ​

Syntax: sample(x, size = , replace = ) ​

where: ​

  1. x = (equivalent to population) Dataset or a vector of more than 1 element from which sample needs to be chosen
  2. size = Size of the sample
  3. size = Size of the sample

We use set.seed() function to set a seed. We specify any integer in the function as a seed. ​

# setting a seed set.seed(20) # Generating a sample of 10 random numbers between 1 and 30 by setting a seed without replacement (i.e. every value will be unique) sample(1:30, 10, replace = FALSE)

6 11 24 2 25 27 13 9 3 28

Note: The random numbers generated remains constant even after multiple executions. ​

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks