R Factor
Factor
- A Factor is a vector object used to specify a discrete classification (grouping) of the components of other vectors of the same length
- Factor variables are categorical variables that can be either numeric or string type
- R provides both ordered and unordered factors
Syntax
//Syntax:
>factor(x)
>as.factor(x)
x can be a vector, list, matrix or a dataframe
it checks if an object is a factor or not and returns a Boolean value
CODE/PROGRAM/EXAMPLE
var <- c(1,0,1,0,0,1,0,1,1,0)
var1 <- factor(var)
is.factor(var1)
[1]TRUE
var1
[1] 1 0 1 0 0 1 0 1 1 0
Levels: 0 1
var2 <- as.factor(var)
var2
[1] 1 0 1 0 0 1 0 1 1 0
Levels: 0 1
Consider the following data with different types of waste and convert it to a factor using as
CODE/PROGRAM/EXAMPLE
x <- c('organic', 'metal', 'plastic',
+ 'plastic', 'organic', 'metal', 'metal',
+ 'metal', 'organic', 'paper', 'plastic',
+ 'glass', 'mixed', 'e-waste', 'glass',
+ 'glass', 'e-waste', 'glass', 'metal')
Output:
Factors give us the following information
- The Levels gives us the unique values in the dataset
- table(x) gives us the frequency of individual items available
- level(x) gives us the levels of the factor
Ordered Factor
In an Ordered Factor, logical flag is set to determine if the levels should be regarded as ordered or not
Consider a vector named ‘depth’. By using the option ‘ordered = TRUE’, it can be converted to an ordered factor
CODE/PROGRAM/EXAMPLE
#UnOrdered Factors
depth <- c("Low", "High", "Medium", "High",
"High", "Medium", "Medium", "Low",
"Low", "Low", "High", "Low")
depth.f <- factor(depth)
Output:
CODE/PROGRAM/EXAMPLE
#Ordered Factors
depth.f <- factor(depth, levels = c("Low", "Medium", "High"), ordered = TRUE)
Output: