Create Categories Based On Integer & Numeric Range in R (2 Examples)

 

In this tutorial you’ll learn how to construct categorical variables based on integers and numeric ranges in R programming.

The tutorial contains this information:

Let’s dig in!

 

Example 1: Convert Integer into Categorical Data

Example 1 illustrates how to create a categorical vector using an integer (e.g. a person’s age in years) as input data.

Consider the following integer vector in R:

num1 <- c(1, 4, 3, 7, 1, 4, 3, 3, 7)    # Create numeric example data
num1                                    # Print numeric example data
# [1] 1 4 3 7 1 4 3 3 7

As you can see in the RStudio console, our example vector contains some random integer values.

Let’s check the class of our data:

class(num1)                             # Class of data
# [1] "numeric"

The data type of our input data is numeric (the same R code could be applied to the integer class).

Now, we can convert this numeric vector to the factor class (i.e. categorical data) using the as.factor function:

cat1 <- as.factor(num1)                 # Convert numeric to factor
cat1                                    # Print categorical data
# [1] 1 4 3 7 1 4 3 3 7
# Levels: 1 3 4 7

Have a look at the previous output of the RStudio console: The values are the same, but the data has been grouped into factor levels.

We can also check the class of our final data object using the class function:

class(cat1)                             # Class of data
# [1] "factor"

It’s a factor!

In this example we have used the actual integer values as categories. However, it is also possible to group a numeric vector into categorical ranges.

You guessed it… That’s what I’m going to show next.

 

Example 2: Convert Numerical Ranges into Categorical Data

Example 2 illustrates how to convert continuous numerical ranges into discrete categories defined by intervals. Let’s first create some random example data in R:

set.seed(6042397)                       # Set random seed
num2 <- rnorm(100)                      # Create numeric example data
head(num2)                              # Head of numeric example data
# [1]  1.2645445 -0.4518619 -0.4574372  0.7558008 -0.4175519 -1.1232298

The previous output shows the first six values of our example vector. As you can see, we have created a vector consisting of random numeric values.

We can now convert this vector into categorical ranges:

cat2 <- numeric()                       # Create empty data object
cat2[num2 < - 1] <- 1                   # Assign categories based on numeric range
cat2[num2 >= - 1 & num2 < 0] <- 2
cat2[num2 >= 0 & num2 < 1] <- 3
cat2[num2 >= 1] <- 4
cat2 <- as.factor(cat2)                 # Convert numeric to factor
head(cat2)                              # Head of categorical data
# [1] 4 2 2 3 2 1
# Levels: 1 2 3 4

The previous output shows our final result: A categorical factor vector with four different categories (i.e. 1, 2, 3, and 4).

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the content of this article. You can find the video below:

 

 

In addition, you might want to have a look at some of the other articles of Statistics Globe.

 

In summary: In this R tutorial you have learned how to convert numeric and integer data to categorical. Let me know in the comments, if you have any additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top