R Factors

R Factors

🏷️ R Factors

Factors in R are used to store categorical data (data that belongs to fixed groups or categories).

They are very important in data analysis, statistics, and modeling.


 1. What is a Factor?

  • A special data type for categorical variables

  • Stores data as levels

  • More memory-efficient than character vectors

  • Widely used in statistical models


 2. Creating a Factor



 


 3. Levels of a Factor



 

Output:

[1] "Female" "Male"

📌 Levels are sorted alphabetically by default


4. Number of Levels



 


 5. Change Factor Levels



 


 6. Ordered Factors

Used when categories have a natural order.


 


 7. Compare Ordered Factors



 

✔ Works only with ordered factors


8. Convert Factor to Character / Numeric


 

⚠️ Direct as.numeric(factor) can give wrong results

 9. Factors in Data Frames


 


 10. Drop Unused Levels



 


 Factors vs Characters

Feature Factor Character
Purpose Categorical data Text
Levels Yes No
Memory Efficient Less efficient
Modeling Preferred Not ideal

 When to Use Factors?

✔ Categorical variables (gender, grade, status)
✔ Statistical models
✔ Grouping & summarization


📌 Summary

  • Factors represent categorical data

  • Stored as levels

  • Ordered factors support comparison

  • Essential for statistical analysis

You may also like...