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 displays level values in a default sorted order, but we can change this by defining 'levels=values' as an argument in a factor.

Creating a Factor

R provides factor() function to create a factor that accepts a vector with values as an 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 a sorting order. We can change this by defining the 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 languages, the element starts with 0 index.

In the given 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 the 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 a new value with the help of the assignment operator. In the given example, we modify the third factor element.

data <- c('Mon','Tues','Wed','Thu','Fri')
fdata <- factor(data)
fdata[2] <- 'sat'
fdata






Read more articles


General Knowledge



Learn Popular Language