Open In App

Categorize Ages into Different Groups in R

Last Updated : 27 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to categorize ages into different groups with its working example in the R Programming Language using R if-else conditions. Categorizing ages into distinct groups is a common task in data analysis and decision-making. In R programming, if-else condition is a versatile tool for achieving this.

If-Else Condition

If-else condition is a control structure that differenciates over a sequence of values and executes a specified block of code for each value. We have a variable age and if we want to categorize ages into different groups within it, we can use a if-else condition.

The practical application of if-else conditions for age categorization is explored in this article, offering tips on how to effectively do this assignment in R. Learning how to categorise people according to their ages using if-else conditions is a useful ability whether you’re working with demographic data or in any other situation where age-related differences are relevant.

Lets explore how to categorize ages into different groups and understand its step-by-step implementation.

Syntax:

if (age < 18) {

category <- “Child”

} else if (age >= 18 && age < 60) {

category <- “Adult”

} else {

category <- “Senior”

}

cat(“Age:”, age, “\n”)

cat(“Category:”, category, “\n”)

Age Categorisation using if-else conditions in R

R




age <- 10
 
if (age < 18) {
  category <- "Child"
} else if (age >= 18 && age < 60) {
  category <- "Adult"
} else {
  category <- "Senior"
}
 
cat("Age:", age, "\n")
cat("Category:", category, "\n")


Output:

Age: 10 
Category: Child
  • We start with the age variable age set to 10.
  • The first if condition checks if age is less than 18. This condition is TRUE
  • The category variable is assigned the value “Child” because the age passes within the specified range.
  • The cat function is used to print the age and the category.
  • The output confirms that the age is 10 and falls into the “Child” category.

Age-Based Categorisation in R using conditional statements

R




age <- 35
 
if (age < 18) {
  category <- "Child"
} else if (age >= 18 && age < 60) {
  category <- "Adult"
} else {
  category <- "Senior"
}
 
cat("Age:", age, "\n")
cat("Category:", category, "\n")


Output:

Age: 35 
Category: Adult
  • We start with the age variable age set to 35.
  • The first if condition checks if age is less than 18. This condition is FALSE, so we move to the next condition.
  • The else if condition checks if age is greater than or equal to 18 AND less than 60. Since 35 satisfies this condition (TRUE), the code inside the else if block is executed.
  • The category variable is assigned the value “Adult” because the age falls within the specified range.
  • The cat function is used to print the age and the category.
  • The output confirms that the age is 35 and falls into the “Adult” category.

Age-Based Categorization in R Using cut() Function

R




age <- 45
 
age_ranges <- c(0, 18, 60, Inf)
categories <- c("Child", "Adult", "Senior")
 
category <- cut(age, breaks = age_ranges, labels = categories, right = FALSE)
 
cat("Age:", age, "\n")
cat("Category:", as.character(category), "\n")


Output:

Age: 45 
Category: Adult
  • We define the age_ranges vector to represent the age breaks. The vector includes 0, 18, and Inf (infinity) to define the age ranges for “Child,” “Adult,” and “Senior.”
  • The categories vector holds the corresponding category labels.
  • We use the cut() function to categorize the age into the specified ranges using the breaks and labels arguments. The right argument is set to FALSE to include the left boundary in each range.
  • The category variable will hold the assigned category based on the age.
  • The cat function is used to print the age and the assigned category.

Conclusion

In summary, this article has delved into the categorization of ages into distinct groups using R’s if-else conditions and the cut() function. Whether for demographic analysis or any scenario necessitating age-based differentiation, mastering these techniques is invaluable. While if-else conditions are suitable for simpler cases, the cut() function provides flexibility for more complex tasks and large datasets. Efficient age categorization in R empowers robust data analysis and decision-making, with the choice between methods hinging on project complexity and dataset size.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads