Create a Factor in R

ProblemCreate a Factor for categorical variables in R

Solution:

What is a Factor variable?

A Factor refers to a statistical data type that can be used to store categorical variables

So, what is a categorical variable?

A Categorical Variable corresponds to a variable which has limited number of categories.

Example:A good example of a categorical variable is the variable ‘Gender‘. A human individual can either be “Male” or “Female” and thus choices are limited.

To create factors in R, we make use of the function factor().

First, create a vector that contains all the observations of the Genders. It is clear that there are two categories, or in R-terms ‘Levels’, i.e. “Male” and “Female”.

There are two types of categorical variables: 
  • nominal categorical variable: A categorical variable without an order. For example, let’s consider a Vector of Ice Cream Flavors like “V” for “Vanilla”, “S” for “Strawberry” and “C” for “Chocolate”. Here, it is not possible to say if one of the flavors stands above or below the other.
  • ordinal categorical variable: A categorical variable with a natural ordering. For example, let’s consider a Vector of Temperature with elements like “Low”, “Medium” and “High”. Here, it is obvious that “High” stands above “Medium” and “Medium” stands above “Low”.
If you are learning to create a Factor in R, you may like the below posts too.

Happy Learning!

0

Leave a Reply

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