Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

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 responses with 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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.