R Factors

R Tutorial

 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


 

  • Warning: Direct as.numeric(factor) can give wrong results

 9. Factors in Data Frames


 


 10. Drop Unused Levels


 Factors vs Characters

FeatureFactorCharacter
PurposeCategorical dataText
LevelsYesNo
MemoryEfficientLess efficient
ModelingPreferredNot 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...