What does cut do in R

This recipe explains what does cut do in R

Recipe Objective

What does cut do?

The cut function is used in R for cutting a numeric value into bins of continuous values and is specified with cut labels. **Syntax: cut (x, breaks, labels, include.lowest, right)** where, x: numeric input value (vector) Break : No of breaks points Labels: labels to each group/bin include.lowest: whether to include the lowest (FALSE) or the highest (TRUE) break value or not. Right: interval should be closed on the right and open on the left or vice versa. This recipe demonstrates an example of cut () in R.

Learn How to Build a Multi Class Text Classification Model using BERT

Step 1 - cut() - break argument

x <- c(0:10) cut(x, breaks=2) # The breaks argument cuts the data into bins.

"Output of code :"
(-0.01,5](-0.01,5](-0.01,5](-0.01,5](-0.01,5](-0.01,5](5,10](5,10](5,10](5,10](5,10]
 Levels:
'(-0.01,5]''(5,10]'

Step 2 - cut() - using labels arguement

The labels argument, the data element falls into the labeled bin. In the following example, the labels are for bins ranging in : First : 0-7 Second : 8-15 Third : 16-24 Fourth : 25-34 Fifth : 35-50

x <- c(3,19,27,49,22,16) cut(x, breaks = c(0,7,15,24,34,50), labels = c("First", "Second", "Third", "Fourth", "Fifth"))

"Output of code :"
First Third Fourth Fifth Third Third

Step 3 - cut() - using include.lowest argument

include.lowest =FALSE , returns the values with lowest value are not included in the intervals. include.lowest =TRUE , returns the values with lowest value are included in the intervals.

x <- 0:20 cut(x, breaks = c(0, 10, 20), include.lowest = FALSE)

"Output of code :"
 (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20]

cut(x, breaks = c(0, 10, 20), include.lowest = TRUE)

"Output of code :"
[0,10] [0,10] [0,10] [0,10] [0,10] [0,10] [0,10] [0,10] [0,10] [0,10] [0,10] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20]

Step 4 - cut() - right argument

right = TRUE : the intervals are opened at the left and closed at the right (a,b] right = FALSE : the intervals are opened at the right and closed at the left [a,b)

x <- 0:20 cut(x, breaks = c(0, 10, 20),right=TRUE)

"Output of code :"
 (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (0,10] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20] (10,20]

x <- 0:20 cut(x, breaks = c(0, 10, 20),right=FALSE)

"Output of code :"
[0,10) [0,10) [0,10) [0,10) [0,10) [0,10) [0,10) [0,10) [0,10) [0,10) [10,20) [10,20) [10,20) [10,20) [10,20) [10,20) [10,20) [10,20) [10,20) [10,20) 

{"mode":"full","isActive":false}

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

A/B Testing Approach for Comparing Performance of ML Models
The objective of this project is to compare the performance of BERT and DistilBERT models for building an efficient Question and Answering system. Using A/B testing approach, we explore the effectiveness and efficiency of both models and determine which one is better suited for Q&A tasks.

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

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.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch