R Factors

What are Factors?

Factors represent categorical data in R.

They store values as levels (categories).

Loading...
Output:

Viewing Levels

Loading...
Output:

Factor Structure

Loading...
Output:

Creating Factors with Levels

Loading...
Output:

Ordered Factors

Loading...
Output:

Sorting Factors

Loading...
Output:

Using Factors in Data Frames

Loading...
Output:

Changing Levels

Loading...
Output:

Full Example

Loading...
Output:

Why Factors are Important

  • Represent categorical data
  • Used in statistics
  • Improve performance
  • Required for modeling

Common Mistakes

  • Treating factors like strings
  • Not defining levels
  • Ignoring ordering

Practice

Create a factor for grades, define levels, and sort it.

Loading...
Output:

Need Help?

Ask the AI if you need help understanding or want to dive deeper in any topic