R Factors
Factor is a special variable type for storing categorical variables as levels, it is generally used in statistical modeling. It may contain both numeric and character values, but the factor levels will always be character values. The factor display level values in default sorted order, but we can change this by defining 'levels=values' as an argument in factor.
Creating a Factor
R provides factor() function to create a factor, that accepts vector with values as argument. The example below contains working day names in factor.
data <- c('Mon','Tues','Wed','Thu','Fri')
fdata <- factor(data)
print(fdata)
[1] Mon Tues Wed Thu Fri
Levels: Fri Mon Thu Tues Wed
In the above example, the week names are displaying by default in sorting order. We can change this by defining level argument, like-
data <- c('Fri','Mon','Tues','Wed', 'Mon','Thu','Tues','Fri')
fdata <- factor(data, levels=c('Mon','Tues','Wed','Thu','Fri'))
print(fdata)
[1] Fri Mon Tues Wed Thu Tues Fri
Levels: Mon Thu Wed Tues Fris
Access Element of Factor
We can access a factor element by using its index, as it starts with index 1 unlike, in other programming language element starts with 0 index.
In the below example, we have accessed the second factor element.
data <- c('Mon','Tues','Wed','Thu','Fri')
fdata <- factor(data)
fdata[2]
[1] Tues
Levels: Fri Mon Thu Tues Wed
Get Length of a Factor
R provides length() function to return the length of a factor.
data <- c('Mon','Tues','Wed','Thu','Fri')
fdata <- factor(data)
length(fdata)
[1] 5
Delete a Factor
To delete a factor element, simply assign it to a null value.
data <- c("Mon", "Tues", "Wed", "Thu", "Fri")
fdata <- factor(data)
fdata <- NULL
Modify a Factor Element
To modify a factor element, simply assign the element index with new value with the help of assignment operator. In the below example, we modify the third factor element.
data <- c('Mon','Tues','Wed','Thu','Fri')
fdata <- factor(data)
fdata[2] <- 'sat'
fdata