Naming &
Formatting
Learn to write clean, readable R code by choosing meaningful variable names and following consistent formatting conventions.
Why Does Naming Matter?
Code is read far more often than it is written. When you revisit your R script six months later, or share it with a classmate, the names you choose determine whether the code is instantly understandable or a confusing puzzle.
What Is a Variable Name?
In R, a variable name (also called an identifier) is the label you assign to a stored value using the assignment operator <-. The name is how you refer to that value later in your script.
What Is Code Formatting?
Formatting refers to the visual layout of your code: how you use whitespace, indentation, line breaks, and alignment to make your script easy to scan and understand.
The Golden Rule of Naming
R Naming Conventions
R programmers use several conventions. The tidyverse style guide recommends snake_case as the standard for variable and function names.
snake_case vs camelCase
| snake_case | camelCase | |
|---|---|---|
| Word separator | Underscore ( _ ) | Capital letter |
| Example | total_sales | totalSales |
| Tidyverse standard | ✓ Yes | ✕ No |
| Readability | Higher (underscores act as spaces) | Good, but harder to scan |
| Used in base R | Some functions | Some functions |
| Best for | New R projects | Matching existing codebase |
Core Formatting Rules
Indentation & Braces
Proper indentation shows the logical structure of your code. In R, the opening brace { goes on the same line as the control statement, and the closing brace } gets its own line.
How to Apply These Rules
<-, +, ==, and after every comma.{}.styler::style_file("my_script.R") to auto-format your code.Example 1: Basic Naming
A student writes a script to calculate the average of quiz scores. Let's improve the variable names.
Example 2: Formatting a Function
This function reads a CSV file and filters rows. Watch how spacing and indentation transform the readability.
Example 3: A Complete Script
Putting it all together: a well-formatted R script that loads data, processes it, and outputs results.
Comments & Documentation
Good names reduce the need for comments, but comments still have an important role: they explain why, not what.
Common Mistakes
Quick Reference Table
| Rule | Bad | Good |
|---|---|---|
| Variable names | x, d2, myVal | avg_score, total_count |
| Assignment | x = 5 | x <- 5 |
| Operator spacing | y<-x+1 | y <- x + 1 |
| Comma spacing | c(1,2,3) | c(1, 2, 3) |
| Indentation | Tabs / 4 spaces | 2 spaces per level |
| Line length | > 120 characters | < 80 characters |
| Comments | # set x to 5 | # FDA max limit (ppb) |
Question 1 of 5
Which variable name follows the tidyverse snake_case convention?
Question 2 of 5
What is wrong with using c as a variable name in R?
Question 3 of 5
Which line of R code has correct formatting according to the tidyverse style guide?
Question 4 of 5
A classmate writes: d <- read.csv("survey.csv"). Which rename is the best improvement?
Question 5 of 5
What is the recommended indentation style in R according to the tidyverse guide?