Chapter 10: R Variable Names

R Variable Names (also called object names or identifiers).

Naming variables well is one of the most important skills in R — even more than knowing fancy functions. Bad names make code hard to read in 2 weeks; good names make it feel like self-documenting English.

I’ll explain everything like we’re sitting together in RStudio, going line by line — rules first, then best practices 2026, lots of good/bad examples, and real mini-scripts you can copy-paste.

1. The Strict Syntax Rules (What R Actually Allows)

R has very clear legal rules for variable names. If you break them → immediate error.

Rule Allowed (Good) Not Allowed (Error) Explanation
Must start with letter (a–z, A–Z) or . 1st_place, 2026_sales, -temp Cannot start with number or most symbols
After first character letters, numbers, _, . student-name, price@tax, total space No -, @, #, $, spaces, %, &, etc.
Case-sensitive Age ≠ age ≠ AGE student and Student are completely different
Length practically unlimited (but be reasonable) Very long names are legal but painful to type
Reserved words if, else, TRUE, function, NA, Inf, NULL, for, while, break, next, return You cannot use language keywords as names
Special exceptions . alone or ..1, ..2 etc. . is legal but almost never used for normal variables (used for hidden/temp objects)

Quick test you can run right now:

R

2. Real-World Best Practices & Conventions (2026 Reality)

The legal rules are loose — but the community has very strong opinions about what makes code readable.

Today (2026) the dominant convention — especially in data science, tidyverse world, most universities, journals, and new packages — is the tidyverse style guide (written by Hadley Wickham, still the gold standard):

  • Use only lowercase letters, numbers, and _ (underscore)
  • Separate words with _ → called snake_case
  • Variable names → nouns (what it contains)
  • Function names → verbs (what it does)
R

Why snake_case won in modern R:

  • Very readable (words clearly separated)
  • No confusion with S3 methods (which use . like print.myclass)
  • Matches column names in tidy data (dplyr, tidyr, etc.)
  • Easy to type (no need to hold Shift for capitals)
  • Used in: tidyverse packages, r4ds book, most recent textbooks, Posit training

3. Other Styles You Will Still See (and When)

Style Example Where You See It Pros Cons / Why Avoid in 2026
snake_case total_revenue, student_marks tidyverse, dplyr pipelines, most new code Most readable, modern standard None really
dot.case total.revenue, student.marks base R, old code, some legacy packages Looks like data.frames Confusing with S3 methods (print.data.frame)
camelCase totalRevenue, studentMarks Bioconductor, some Shiny apps, older teaching Familiar from Java/JS Harder to read long names
UpperCamelCase (PascalCase) TotalRevenue, StudentMarks Class names, some function names (Google style) Clear for classes Not for variables

Bottom line 2026: If you’re learning new R, doing data analysis, using tidyverse/ggplot2/dplyr → snake_case is the way. Consistency in one project matters more than which style — but snake_case is the safest bet right now.

4. Good vs Bad Naming – Real Examples

Bad (hard to understand later):

R

Good (self-explaining):

R

Even better (with context):

R

5. Quick Tips from Experienced R Users

  • Be descriptive but not crazy long → customer_lifetime_value > clv but < customer_lifetime_monetary_value_calculated_on_2026_data
  • Avoid numbers in names if possible → instead of sales_2025, sales_2026 → put year in a variable or filter
  • Use plural for collections → student_marks (vector), students (data frame)
  • Logical variables → start with is_, has_, can_ → is_weekend, has_missing_values
  • Constants → ALL_CAPS → MAX_TEMPERATURE, PI
  • Never use . for normal variables (save for S3 classes/methods)

Your Mini Practice (Copy → Run & Rename!)

R

Now the code tells a story without comments!

Questions?

  • Want to fix naming in one of your old scripts together?
  • Go deeper into why . is dangerous in names?
  • Next topic (vectors, subsetting, data frames…)?

Just tell me — whiteboard is ready! ☕🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *