Factor in R - Categorical & Continuous Variables
Factor in R - Categorical & Continuous Variables
(/)
In a categorical variable, the value is limited and usually based on a particular finite
group. For example, a categorical variable can be countries, year, gender, occupation.
A continuous variable, however, can take any values, from integer to decimal. For
example, we can have the revenue, price of a share, etc..
Categorical Variables
R stores categorical variables into a factor. Let's check the code below to convert a
character variable into a factor variable. Characters are not supported in machine
learning algorithm, and the only way is to convert a string to an integer.
Syntax
Arguments:
/
26/01/2020 Factor in R: Categorical & Continuous Variables
Example:
Output:
## [1] "character"
## [1] "factor"
It is important to transform a string into factor when we perform Machine Learning task.
A categorical variable can be divided into nominal categorical variable and ordinal
categorical variable.
Output:
Example:
Output:
Example:
## Levels: morning < midday < afternoon < evening < midnight
# Append the line to above code
# Count the number of occurence of each level
summary(factor_day)
Output:
R ordered the level from 'morning' to 'midnight' as specified in the levels parenthesis.
Continuous Variables
Continuous class variables are the default value in R. They are stored as numeric or
integer. We can see it from the dataset below. mtcars is a built-in dataset. It gathers
information on different types of car. We can import it by using mtcars and check the
/
26/01/2020 Factor in R: Categorical & Continuous Variables
class of the variable mpg, mile per gallon. It returns a numeric value, indicating a
continuous variable.
Output
## [1] "numeric"