Chapter 7: R Variables
R Variables. This is one of the very first things every beginner must really understand deeply, because almost everything else in R builds on variables.
Imagine we’re sitting together with RStudio open on my laptop (you can follow along in yours right now). I’ll explain slowly, like a patient teacher in a classroom near Hitech City, with lots of examples, why things work this way, common mistakes (especially ones Indian students make), and real code you can copy-paste.
1. What is a Variable in R? (Super Simple Definition)
A variable in R is just a named box (or container) where you store a value (or many values).
- You give it a name (like temperature, marks, student_name)
- You put something inside it (a number, text, table, model, plot…)
- Later you can use that name to get the thing back or change it
In R, variables are very flexible — one variable can hold a single number today and a huge table tomorrow. No need to declare type first (unlike C++/Java).
2. How to Create (Assign) a Variable – The Famous <-
R has three ways to assign, but modern R users almost always use <-
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# Best & most common style (recommended by tidyverse, books, everyone in 2026) temperature <- 29.5 # stores number 29.5 student_name <- "Webliance" # stores text (character/string) passed_exam <- TRUE # stores logical (TRUE/FALSE) marks <- c(78, 92, 85, 67) # stores vector (list of numbers) # Other two ways (you'll see them, but avoid in new code) age = 24 # = works, but can confuse inside functions 100 -> score # right arrow – rare, looks backward |
Why <- is preferred (important lesson):
- = is also used for naming arguments in functions → using = for assignment can cause bugs
- Example of confusion:
|
0 1 2 3 4 5 6 7 8 9 10 |
# This is OK ( = for argument name) lm(mpg ~ wt, data = mtcars) # This is confusing/wrong in some contexts lm(mpg ~ wt, data = mtcars = new_data) # error or unexpected! |
So: Always use <- for creating variables — it’s clearer and safer.
3. Rules for Variable Names (Naming Conventions – Follow These!)
Valid names must follow these rules (or R throws error):
| Rule | Good Example | Bad Example (error) | Why? |
|---|---|---|---|
| Start with letter or . | x, marks, .hidden | 1st_place, 2marks | Cannot start with number |
| After first char: letters, numbers, _, . | student_1, total.marks | student-name | No -, @, #, spaces, etc. |
| Case-sensitive | Age ≠ age | — | Age and age are different |
| Avoid reserved words | mean, if, TRUE | if <- 5 (error) | Reserved for language keywords |
Best modern style (2026 tidyverse / R community recommendation):
- Use lowercase with _ (snake_case) → student_marks, monthly_sales
- Or camelCase → studentMarks (some people like this)
- Make names descriptive → avg_temperature_hyd better than x
- Use . only for special cases (like S3 methods) or hidden variables (.temp)
4. What Can You Store in a Variable? (The Real Power of R)
Variables in R can hold almost anything:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 1. Atomic / simple types (building blocks) num_var <- 3.14159 # double (most common number type) int_var <- 42L # integer (L forces integer) char_var <- "Hyderabad 2026" # character/string – use " " or ' ' logical_var <- TRUE # or FALSE – uppercase! complex_var <- 2 + 3i # complex numbers (math/science) # 2. Vectors (most important – R lives on vectors!) marks <- c(85, 92, 78, 88) # numeric vector names <- c("Aarav", "Priya", "Rahul") passed <- c(TRUE, TRUE, FALSE, TRUE) # 3. Data frame (Excel-like table – heart of data analysis) students <- data.frame( name = c("Sneha", "Vikram", "Priya"), age = c(22, 24, 21), marks = c(92, 79, 88), city = c("Hyd", "Bng", "Del") ) # 4. Other common things model <- lm(mpg ~ wt, data = mtcars) # linear model my_plot <- ggplot(students, aes(age, marks)) + geom_point() my_list <- list(a = 1, b = "text", c = students) |
See? One variable can hold a number, a list of 1000 rows, or even a fitted machine learning model.
5. How to See / Use / Change a Variable
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# See value (auto-print in console/script) temperature # Explicit print print(student_name) cat("Hello,", student_name, "! Temp is", temperature, "°C\n") # Use in calculation new_temp <- temperature + 2 mean(marks) # Change value later temperature <- 30.1 # overwrites old value – no problem marks[3] <- 90 # change only 3rd element |
6. Removing Variables (When Memory Gets Full or You Want Clean Slate)
|
0 1 2 3 4 5 6 7 8 9 10 |
rm(temperature) # delete one rm(marks, student_name) # delete many # Delete everything (careful!) rm(list = ls()) # clears all user variables – like restart |
In RStudio: Environment pane → click broom icon to clear all.
7. Quick Look at Scoping (Where Variables “Live” – Beginner Level)
For now, just know:
- When you do x <- 10 in console/script → it goes to global environment (your workspace)
- Inside a function, variables are usually local (disappear after function finishes)
- Very simple example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
x <- 100 # global test <- function() { x <- 5 # local – different x! print(x) # prints 5 } test() # 5 print(x) # still 100 – global unchanged |
(We go much deeper into scoping later when we do functions.)
8. Your Mini Practice Right Now (Copy → Run in RStudio)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Create variables – Hyderabad style city <- "Hyderabad" current_temp <- 29.8 humidity <- 62 is_summer <- FALSE favorite_foods <- c("Biryani", "Haleem", "Irani Chai") # Make a small table daily_weather <- data.frame( day = c("Mon", "Tue", "Wed"), temp = c(28.5, 30.2, 29.1), rain = c(FALSE, TRUE, FALSE) ) # Use them cat("Welcome to", city, "! Current temp:", current_temp, "°C\n") cat("Favorite foods:", paste(favorite_foods, collapse = ", "), "\n") # Average temp this week avg_temp <- mean(daily_weather$temp) cat("Average temp:", round(avg_temp, 1), "°C\n") # Add new column daily_weather$hot <- daily_weather$temp > 29.5 print(daily_weather) |
You just created, used, and modified different kinds of variables — perfect start!
Summary Cheat-Sheet (Keep This Handy)
- Assignment → <- (best), =, ->
- Names → start with letter/., then letters/numbers/_/., case-sensitive, descriptive snake_case
- Can hold → numbers, text, TRUE/FALSE, vectors, data.frames, models, plots…
- See → just type name or print() / cat()
- Delete → rm()
- Global vs local → global for now (functions later)
Feeling confident?
Next questions:
- Want to go deeper into vectors (because they’re everywhere)?
- Data frames in detail?
- Types/coercion (what happens when you mix numbers + text)?
- Or any line giving error right now?
Just tell me — next whiteboard page is ready! ☕🚀
