 # R Factors

In this tutorial, we will learn about factors in R with the help of examples.

A Factor is a data structure that is used to work with categorizable datas.

Suppose a data field such as marital status may contain only values from single, married, separated, divorced, or widowed.

In such a case, we know the possible values beforehand and these predefined, distinct values are called levels of a factor.

## Create a Factor in R

In R, we use the `factor()` function to create a factor. Once a factor is created, it can only contain predefined set values called levels.

The syntax for creating a factor is

``factor(vector)``

Here, `factor()` takes a vector as an argument.

Let's see an example,

``````# create a factor
students_gender <- factor(c("male", "female", "male", "transgender", "female"))

# print the marital_status factor
print(students_gender)``````

Output

``` male   female   male   transgender    female
Levels: female male transgender```

In the above example, we have used the `factor()` function to create the factor named students_gender.

Notice while printing students_gender, we get two outputs

• All the vectors items
• predefined possible values we know beforehand i.e. levels of students_gender

## Access Factors Elements

Accessing vector elements is similar to that of vectors. We use the index number. For example,

``````# create a factor
students_gender <- factor(c("male", "female", "male", "transgender", "female"))

# access 1st element of students_gender
print(students_gender)

# access 4th element of students_gender
print(students_gender)``````

Output

``` male
Levels: female male transgender
 transgender
Levels: female male transgender```

In the above example, we have used the index number to access elements of the students_gender

• students_gender - returns the 1st element of students_gender i.e `"male"`
• students_gender - returns the 4th element of students_gender i.e. `"transgender"`

Note that each time we access and print factor elements we get a level of factor too.

## Modify Factor Element

To change a vector element, we can simply reassign a new value to the specific index. For example,

``````# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

# print the marital_status factor
marital_status <- "divorced"

print(marital_status)``````

Output

``` divorced
Levels: divorced married single```

Here, we have reassigned a new value to index 1 of the marital_status factor to change the element from `"married"` to `"divorced"`.

How to find the length of a factor in R?

In R, we use the `length()` function to find the number of items present in a factor. For example,

``````# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

cat("Total Elements:", length(marital_status))``````

Output

`Total Elements: 5`
How to loop over a factor in R?

In R, we can also loop through each element of the factor using the for loop. For example,

``````# create a factor
marital_status <- factor(c("married", "single", "single", "divorced", "married"))

# iterate through each elements of marital_status
for (status in marital_status) {
print(status)
}``````

Output

``` "married"
 "single"
 "single"
 "divorced"
 "married"```