R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
12: Factors
Why can't I just use character strings for categories?
I get this question a lot. If you have a column of "Low", "Medium", and "High", why bother wrapping it in factor() when a simple character vector works? The short answer is that factors aren't just strings; they are integers with labels. R stores the unique categories (the "levels") once and then uses numbers to represent them in the data.
Beyond saving memory, the real power is that factors tell R that the data is categorical. This is huge when you get into statistical modeling or plotting. If you tell R a variable is a factor, it knows how to treat it as a grouping variable rather than just a piece of text. Without factors, R has no idea that "Low" comes before "Medium".
# Just strings (no inherent order)
sizes <- c("Small", "Large", "Medium", "Small")
# A factor (R now knows these are specific categories)
size_factor <- factor(sizes)
print(size_factor)
# Output will show: Levels: Large Medium Small
My categories are alphabetized, but they should be in a specific order. How do I fix this?
By default, R is helpful—too helpful. It looks at your character vector and assigns levels alphabetically. This is a nightmare when you're dealing with something like "Small", "Medium", and "Large", because R will put "Large" first. I've spent way too many hours debugging plots that looked wrong simply because the axis was alphabetized.
To fix this, you have to explicitly define the levels argument. This tells R, "Ignore the alphabet; this is the exact order I want."
# This is the wrong way (alphabetical)
sizes <- c("Small", "Large", "Medium", "Small")
wrong_factor <- factor(sizes)
# Levels: Large Medium Small
# This is the right way
correct_factor <- factor(sizes, levels = c("Small", "Medium", "Large"))
print(correct_factor)
# Levels: Small Medium Large
Now, if you pass correct_factor into a ggplot or a summary table, it will respect that logical order.
Why does converting a factor to a number give me the wrong values?
This is probably the most frustrating "gotcha" in all of R. Let's say you have a factor that represents ratings (1, 2, 3, 4, 5) but it was imported as a factor. If you try as.numeric() on it, you might expect to get the numbers back. Instead, you get the internal integer codes.
Since R stores factors as integers (1, 2, 3...) mapped to labels, as.numeric() strips the labels and gives you the raw integers. If your labels were "10", "20", "30", as.numeric() would give you 1, 2, 3. It's a total trap.
# The Trap
ratings <- factor(c("10", "20", "10", "30"))
as.numeric(ratings)
# Result: 1 2 1 3 (Wait, what?!)
# The Fix: Convert to character first, then to numeric
as.numeric(as.character(ratings))
# Result: 10 20 10 30
Just remember: if it's a factor and you want the actual value printed on the label, go through as.character() first. Always.
📋 Practical Task
Reordering the Customer Satisfaction Survey
You have been given a vector of survey responses from a recent customer satisfaction poll. Currently, the data is stored as a character vector, and when you try to analyze it, R is sorting the results alphabetically (Excellent, Fair, Good, Poor), which makes the report useless.
Your Task:
- Create a character vector called
responseswith these values:"Good", "Poor", "Excellent", "Fair", "Good", "Excellent". - Convert this vector into a factor called
satisfaction_factor. - Ensure the levels are explicitly set in the following logical order:
"Poor", "Fair", "Good", "Excellent". - Print the
levels()of your new factor to verify the order is correct.
There are no comments for now.