Open In App

Cut() Function in R

Last Updated : 02 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Cut() function in R Programming Language is used to divide a numeric vector into different ranges. It is particularly useful when we want to convert a numeric variable into a categorical one by dividing it into intervals or bins.

Syntax of cut() function in R

cut.default(x, breaks, labels = NULL, include.lowest = FALSE, right = TRUE, dig.lab = 3)

Parameters:

  • x: Numeric Vector
  • break: break points of the vector
  • labels: labels for levels
  • include.lowest: Boolean value to include lowest break value
  • right: Boolean value to close interval on the right
  • dig.lab: Used when labels are not provided

Example 1: Creating Age Groups Using cut()

Let’s start by creating a numeric vector and applying the cut() function to divide it into distinct age groups:

R
# Create a numeric vector
ages <- c(18, 25, 35, 40, 50, 60, 70, 80, 90)

# Cut the vector into age groups
age_groups <- cut(ages, breaks = c(0, 25, 50, 75, 100), 
                  labels = c("18-25", "26-50", "51-75", "76-100"))

print(table(age_groups))

Output:

age_groups
18-25 26-50 51-75 76-100
2 3 2 2

Here, the ages vector is split into four age categories: "18-25", "26-50", "51-75", and "76-100". The table() function is then applied to count the occurrences in each group.

Example 2: Creating a Data Frame with Age Groups

Here, we will create a data frame that includes the age category of each person and print the output in a clean format.

R
ages <- c(18, 25, 35, 40, 50, 60, 70, 80, 90)

# Cut the vector into age groups
age_groups <- cut(ages, breaks = c(0, 25, 50, 75, 100), 
                  labels = c("18-25", "26-50", "51-75", "76-100"))

# Create a data frame with the result
result_df <- data.frame(AgeGroup = levels(age_groups), Count = table(age_groups))

print(result_df)

Output:

AgeGroup Count.age_groups Count.Freq
1 18-25 18-25 2
2 26-50 26-50 3
3 51-75 51-75 2
4 76-100 76-100 2

This code segments the ages into predefined age groups and stores the frequency of each group in a data frame for a more structured output.

Example 3: Applying cut() to a Data Frame

Here’s an example where we apply the cut() function to divide a dataset into categories based on age, and create a factor for further analysis.

R
# R program to divide vector into ranges 

# Creating vectors 
age <- c(40, 49, 48, 40, 67, 52, 53) 
salary <- c(103200, 106200, 150200, 10606, 10390, 14070, 10220) 
gender <- c("male", "male", "transgender", 
            "female", "male", "female", "transgender") 
    
# Creating data frame named employee 
employee<- data.frame(age, salary, gender) 
    
# Creating a factor corresponding to age with labels 
wfact = cut(employee$age, 3, labels = c('Young', 'Medium', 'Aged')) 
table(wfact) 

Output:

wfact
Young Medium Aged
4 2 1

Here, we used the cut() function to categorize employee ages into three groups: "Young", "Medium", and "Aged". The result shows the frequency of employees in each age group.

Related Articles:


Next Article
Article Tags :

Similar Reads