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.

Categorize Ages into Different Groups in R

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.

Similar Reads

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....

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")...

Age-Based Categorisation in R using conditional statements

...

Age-Based Categorization in R Using cut() Function

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")...

Conclusion

...

Contact Us