Friday 10 August 2018

Data Management : Factors in R Language

Categorical variables

Quantitative variables
Example:
Height (in meters) - 1.65, 1.76, ....

Qualitative variables
Example:
Gender - Male, Female
Performance - Excellent, Good, Average, Bad ....

Categorical variables
Example:
x : Gender - Male, Female
x = 0 if a person is male
x = 1 if a person is female

Example:
The categories are stored internally as numeric codes, with labels to provide meaningful names for each code.

Factors

Factors represent categorical variables and are used as grouping indicators.

Example:
Suppose we denote the three colors of balls in a basket by following numbers:
Red = 1,  Blue = 2,  Green = 3

Suppose we draw five balls with following colors:
Red, Green, Green, Blue, Red

This outcome of colors can be coded by numbers


Each character is mapped to a code.

Factors represent categorical variables and are used as grouping indicators.

The categories are stored internally as numeric codes, with labels to provide meaningful names for each code.

The order of the labels is important.
First label is mapped to code 1.
Second label is mapped to code 2 and so on.

The values of the codes are always restricted to 1,2,...,k, to represent k discrete categories.

Here "Red" is mapped to code 1,
"Blue" is mapped to code 2 and 
"Green" is mapped to code 3.

We have a vector to character strings or integers.
R's term for a categorical variable is a factor.
In R, each possible value of a categorical variable is called a level.
A vector of level is called a factor.

A categorical variable is characterized by a (here : finite) number of levels called as factor levels.

To define a factor, we start with
  • a vector of values,
  • a second vector that gives the collection of possible values, and 
  • a third vector that gives labels to the possible values.
A factor function encodes the vector of discrete values into a factor:
  factor (x)
          where x is a vector of strings or integers.
If the vector contains only a subset of possible values and not the entire values, then include a second argument that gives the possible levels of the factor:
  factor (x, levels)

Usage
factor (x = character ( ) , levels , labels = levels, exclude = NA, ..)


  • levels : Determines the categories of the factor variable.                         Default is the sorted list of all the distinct values of x.
  • labels : (Optionally Vector of values that will be the labels of the categories in the levels argument.
  • exclude : (Optional) It defines which levels will be classified as NA in any output using the factor variable. 

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (112) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (85) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (18) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (43) Meta (18) MICHIGAN (4) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (719) Python Coding Challenge (155) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses