Naming Conventions
in R Programming
Learn how to write clear, readable variable names and avoid accidentally masking built-in R functions: the foundation of clean code.
What Are Naming Conventions?
A naming convention is a set of rules for choosing the names you give to variables, functions, and data objects in your code. Good names make your code self-documenting: anyone reading it can understand what each piece does.
Why Naming Matters in R
student_avg is easier to trace than x.mean, sum, or c, breaking your code silently.R's Naming Rules
Popular Naming Styles in R
| Style | Example | Usage |
|---|---|---|
| snake_case | student_score |
Most recommended |
| dot.case | student.score |
Traditional R style |
| camelCase | studentScore |
Common in other languages |
| PascalCase | StudentScore |
Less common in R |
| ALL_CAPS | MAX_SIZE |
Constants only |
What Is Function Masking?
Masking occurs when you create a variable with the same name as a built-in R function. R will use your variable instead of the function, silently breaking your code.
The R Search Path
When you use a name like mean, R searches for it in a specific order of environments. This is called the search path. It always starts with your current workspace, the Global Environment.
Safe vs. Dangerous Names
Functions to Never Mask
mean or sum hides them.data() loads datasets. df is the F-distribution function.How to Fix a Masked Function
rm(mean) to delete your variable and restore the original function.base::mean(x) to explicitly call the function from the base package.avg_score instead of mean.Naming Checklist
student_ages not x.max_value.c, mean, sum, data, T, F aren't overwritten.avg_test_score is good; the_average_of_all_test_scores is too long.Example 1: Basic Naming
Task: Store the ages of five students and calculate their average age.
Example 2: Avoiding Masking
Task: Create a vector of test scores, compute the sum, and then compute the mean.
Example 3: Full Analysis
Task: Load a dataset, filter rows, and compute a summary statistic: all with clean naming.
Common Mistakes
x, y, a for everything. Fine in short math formulas, but terrible for data analysis scripts.c <- 5 or T <- "text". These are the most common silent errors in R.myVar, my_var, and my.var in the same script. Pick one style and stick with it.Summary: Naming Best Practices
| Principle | Bad Example | Good Example |
|---|---|---|
| Descriptive | x |
student_count |
| snake_case | MyData |
my_data |
| No masking | mean <- 5 |
avg_val <- 5 |
| No reserved words | TRUE <- 0 |
is_valid <- TRUE |
| Concise | the_total_number_of_all_items |
item_count |
Question 1 of 5
Which of the following is a valid R variable name?
Question 2 of 5
What happens if you run c <- 10 and then try to use c(1, 2, 3)?
Question 3 of 5
Which naming style is recommended by the tidyverse style guide for R?
Question 4 of 5
You accidentally ran mean <- 50. How do you restore the original mean() function?
Question 5 of 5
Which of the following variable names is most likely to cause a silent bug in your R code?