Hi, I am Mike Marin and
in this video we will talk about how to convert a numeric variable in to a categorical variable in R
There are many reasons one may wish to do this
for example, maybe you want to make some cross-tabulations for this variable
or, maybe you are fitting a regression model and the linearity assumption is not valid
and therefore you like to transform it into a categorical variable
There are many reasons one can think of for wanting to convert a numeric variable into a categorical variable or factor
We will be working with the LungCap data that was introduced earlier in this series of videos
I have already gone ahead and imported the data into R and attached it
We will convert height, a numeric variable, into a categorical variable
to do so we will be using the "cut" command
you can access the help menu by placing a question mark in front of the command name
or by typing the command name in the help search window
Now we are going to create a categorical height variable using
categories A of less than 50
B: 50 to 55, C: 55 to 60, and so on.
To do so we are going to create a new variable called CatHeight
to create this variable we are going to "cut" the Height variable
and the break points we are going to use are 0, 50, 55, 60, 65, 70 and 100.
the zero specifies the lower bound and the one hundred specifies the upper bound.
So here we are going to be creating the categories of less than 50, 50 to 55, 55 to 60,
all the way up to 70 or greater.
By default these intervals are left-open or right-closed;
meaning that border observations go to the left.
For example, an observation of 60 would go in the 55 to 60 bin.
Later we will talk about how to change this.
Next, we want to specify the labels:
these are what the categories are going to be named.
The less than 50 we are going to call category A,
the 50 to 55 we are going to call category B,
and so on,
Now we can submit this command.
Let's take a look at the first 10 observed heights:
as well as the first 10 categorical heights.
Here we can see the height of 62.1 is placed in height category D, the 60 to 65 category;
the height of 74.7 is placed in the category F, the 70-plus category
We have successfully created a categorical representation of height
Now we can change these intervals to be left-closed or right-opened
using the right argument within the "cut" command.
We would do this if we would like an observation of say 60 to be placed in the 60 to 65 category
To do this, in the "cut" command we will use the "right" argument
and set this to false
It's worth noting that this "right" argument
refers to whether or not we want right-closed intervals
setting it equal to false will result in a right-opened interval.
It's also worth noting that it is important to use the "labels" argument
If we do not, by default R will use the intervals themselves as the label
unless we specify what we would like the names of the categories to be
Let's recall the categories we were going to make
Let's also take a look at the first 10 heights
as well as the first 10 categorical heights
Let's take a look at what happens if we do not specify the labels
we just remove that "labels" argument there
and now we can see if we take a look at the first 10 categorical heights
the observation of 62.1 which falls in to previously named category D
is now placed in the 60 to 65 category in R.
The label being used is 60 to 65.
We can see down here the labels or category names that R is using
This is why it is important that we define the labels ourselves
rather than having these weird labels chosen by R
It's also worth noting that rather than specifying the break points for the categories themselves
you can tell R the number of the categories or levels you would like
and R will determine the cut points for the intervals itself.
To do so rather than specifying the break points
we can substitute some integer here,
for example setting breaks equal to 4
would tell R to create 4 categories or levels for this categorical height
R will choose the break points itself
In general, it is recommended that you set the interval break points yourself
to have more control over what the categories look like!
Thanks for watching this video and make sure to check out my other instructional videos.