<commit_before>---
layout: page
title: NULL
---

[R Vocab Topics](index) &#187; Factors

<br>

Factors are used to represent categorical data and can be unordered or ordered. One can think of a factor as an integer vector where each integer has a label. In fact, factors are built on top of integer vectors using two attributes: the `class()`, "factor", which makes them behave differently from regular integer vectors, and the `levels()`, which defines the set of allowed values.  Factors are important in statistical modeling and are treated specially by modelling functions like `lm()` and `glm()`. This tutorial will provide you the basics of managing categorical data as factors.

* <a href="#create">Creating, converting & inspecting</a>
* <a href="#order">Ordering levels</a>
* <a href="#value">Revalue levels</a>
* <a href="#drop">Dropping levels</a>
<br>

<a name="create"></a>

## Creating, Converting & Inspecting

Factor objects can be created with the `factor()` function:

{% highlight r %}
# create a factor string
gender <- factor(c("male", "female", "female", "male", "female"))
gender
## [1] male   female female male   female
## Levels: female male

# inspect to see if it is a factor class
class(gender)
## [1] "factor"

# show that factors are just built on top of integers
typeof(gender)<commit_msg>Update factors.md<commit_after>---
layout: page
title: NULL
---

[R Vocab Topics](index) &#187; Factors

<br>

Factors are used to represent categorical data and can be unordered or ordered. One can think of a factor as an integer vector where each integer has a label. In fact, factors are built on top of integer vectors using two attributes: the `class()`, "factor", which makes them behave differently from regular integer vectors, and the `levels()`, which defines the set of allowed values.  Factors are important in statistical modeling and are treated specially by modelling functions like `lm()` and `glm()`. This tutorial will provide you the basics of managing categorical data as factors.

* <a href="#create">Creating, converting & inspecting</a>
* <a href="#order">Ordering levels</a>
* <a href="#value">Revalue levels</a>
* <a href="#drop">Dropping levels</a>

<br>

<a name="create"></a>

## Creating, Converting & Inspecting

Factor objects can be created with the `factor()` function:

{% highlight r %}
# create a factor string
gender <- factor(c("male", "female", "female", "male", "female"))
gender
## [1] male   female female male   female
## Levels: female male

# inspect to see if it is a factor class
class(gender)
## [1] "factor"

# show that factors are just built on top of integers
typeof(gender)
{'fraction_non_alphanumeric': 0.11673592746505478, 'fraction_numerical': 0.0037778617302606727, 'mean_word_length': 4.807017543859649, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7517748', 'n_tokens_mistral': 773, 'n_tokens_neox': 719, 'n_words': 356}