🏠R Variables & Data Types: Your Digital Treasure Boxes
Imagine you have a room full of treasure boxes. Each box has a name tag on it, and inside you can store different kinds of treasures—numbers, words, or yes/no answers. That’s exactly what variables are in R!
🎯 What You’ll Discover
- How to create and name your treasure boxes (Variables)
- The magic arrows that put treasures inside (Assignment Operators)
- Four types of treasures: Numbers, Integers, Words, and True/False
📦 Variables and Assignment
What Is a Variable?
A variable is like a labeled box where you keep stuff. You give it a name, and R remembers what’s inside.
my_age <- 10
Here, my_age is the name tag and 10 is the treasure inside.
Naming Your Boxes
Good names:
player_score <- 100
favorite_color <- "blue"
is_sunny <- TRUE
Rules:
- Start with a letter (not a number!)
- Use letters, numbers, underscores
_, or dots. - No spaces allowed
graph TD A[You want to store something] --> B[Pick a name] B --> C[Use <- to put it in] C --> D[R remembers it!]
➡️ Assignment Operators
The Magic Arrow: <-
This is R’s favorite way to put things in boxes:
my_pet <- "dog"
Think of <- as an arrow pointing TO the box. The treasure goes INTO the name.
The Equal Sign: =
This works too, but <- is the R tradition:
my_pet = "cat"
Quick Compare
| Operator | Example | Meaning |
|---|---|---|
<- |
x <- 5 |
Put 5 in box x |
= |
x = 5 |
Same thing |
-> |
5 -> x |
Also works! |
Pro tip: Most R programmers use <- because it’s crystal clear!
🔢 Numeric Data Type
Numbers With Decimals
When you store regular numbers (with or without decimals), R calls them numeric:
price <- 19.99
temperature <- 72.5
pi_value <- 3.14159
Check the Type
Want to see what type of treasure you have?
price <- 19.99
class(price)
# Returns: "numeric"
Doing Math
Numeric values love math:
a <- 10.5
b <- 3.2
sum <- a + b # 13.7
🎯 Integer Data Type
Whole Numbers Only
Sometimes you only want whole numbers—no decimals allowed! Add an L to tell R:
apples <- 5L
students <- 30L
The L stands for “Long integer” (a programming tradition).
Why Use Integers?
- Counting things (you can’t have 2.5 students!)
- Saves memory for big data
- Some functions need whole numbers
count <- 42L
class(count)
# Returns: "integer"
Integer vs Numeric
graph TD A[Number] --> B{Has decimal?} B -->|Yes| C[Numeric: 3.14] B -->|No| D{Has L?} D -->|Yes| E[Integer: 5L] D -->|No| F[Still Numeric: 5]
📝 Character Data Type
Words and Text
Want to store words, sentences, or any text? Use quotes:
name <- "Alice"
greeting <- 'Hello, World!'
emoji <- "🎉"
Single ' or double " quotes both work!
Examples
city <- "New York"
favorite_food <- "pizza"
secret_code <- "ABC123"
Check It
my_word <- "sunshine"
class(my_word)
# Returns: "character"
Combining Text
Use paste() to glue words together:
first <- "Hello"
second <- "World"
paste(first, second)
# Returns: "Hello World"
âś… Logical Data Type
True or False—That’s It!
Logical values answer yes/no questions. Only two options:
is_raining <- TRUE
is_monday <- FALSE
Always use CAPITAL letters: TRUE and FALSE
Where Logicals Shine
Perfect for decisions and comparisons:
age <- 15
is_teenager <- age >= 13 & age <= 19
is_teenager # TRUE
Shortcut
You can use T and F as shortcuts:
answer <- T # Same as TRUE
wrong <- F # Same as FALSE
Logical Operations
| Operation | Symbol | Example | Result |
|---|---|---|---|
| AND | & |
TRUE & FALSE |
FALSE |
| OR | | |
TRUE | FALSE |
TRUE |
| NOT | ! |
!TRUE |
FALSE |
🎨 The Complete Picture
graph TD A[R Data Types] --> B[Numeric] A --> C[Integer] A --> D[Character] A --> E[Logical] B --> B1["3.14, 99.9"] C --> C1["5L, 100L"] D --> D1["'hello', 'R'"] E --> E1["TRUE, FALSE"]
đź§Ş Quick Reference Table
| Type | Example | How to Create | Use Case |
|---|---|---|---|
| Numeric | 3.14 |
x <- 3.14 |
Decimals, math |
| Integer | 5L |
x <- 5L |
Counting |
| Character | "hi" |
x <- "hi" |
Text, names |
| Logical | TRUE |
x <- TRUE |
Yes/No |
🚀 You Did It!
Now you know how to:
âś… Create variables with meaningful names
âś… Use <- to assign values
âś… Store decimals as numeric
âś… Store whole numbers as integer with L
âś… Store text as character with quotes
âś… Store yes/no as logical with TRUE/FALSE
Your treasure boxes are ready! Go fill them with amazing data! 🎉