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 <-

R

Why <- is preferred (important lesson):

  • = is also used for naming arguments in functions → using = for assignment can cause bugs
  • Example of confusion:
R

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:

R

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

R

6. Removing Variables (When Memory Gets Full or You Want Clean Slate)

R

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:
R

(We go much deeper into scoping later when we do functions.)

8. Your Mini Practice Right Now (Copy → Run in RStudio)

R

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! ☕🚀

You may also like...

Leave a Reply

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