Open In App

Cut() Function in R

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to Divide a Vector into Ranges in R Programming Languagenusing cut() Function.

cut() function in R

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:

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

Create a numeric vector and apply cut() Function

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 the result
print(table(age_groups))


Output:

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

Cut Vector Using Specific Break Points and Labels

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"))
 
# Create a data frame with the result
result_df <- data.frame(AgeGroup = levels(age_groups), Count = table(age_groups))
 
# Print the result
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

Create a data frame and apply cut() Function

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

The cut() function is versatile and can be applied to various scenarios where binning or categorization of continuous data is needed.



Last Updated : 19 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads