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

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

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.

Deep Learning Project for Text Detection in Images using Python
CV2 Text Detection Code for Images using Python -Build a CRNN deep learning model to predict the single-line text in a given image.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.