๐ฐ Day 1: Basic Programming Magic in R#
Welcome to the Magic Castle where Oda the Otter teaches you the secret spells of programming!
๐ฎ Learning Journey Overview#
Today weโll join Oda on our first adventure into the magical world of R programming! By the end of today, youโll be casting spells with code and making smart decisions like a real data wizard.
๐ฏ Learning Objectives#
๐ค Write your first lines of R code
๐ Understand comments and why theyโre important
๐ฒ Master variables and data types
๐ Control program flow and have computers make decisions using if-else conditions
๐ Letโs Get to Know Each Other!#
๐จ Two Truths and a Creative Lie#
Duration: 10 minutes
How to Play:
For small groups of 3-4 people. Try to sit with people you donโt know very well!
Each person shares 3 things about themselves:
โ Two TRUE things (real facts about you)
๐ญ One CREATIVE LIE (something fun and silly that you made up)
Examples to get you started:
โI have a pet turtle named Mr. Speedyโ
โI can speak three languagesโ
โI once ate 15 pancakes in one sittingโ
โMy favorite color changes every Tuesdayโ
Guess the lie! After each person shares, everyone else tries to figure out which one is the creative lie.
Reveal the lie! Share which was the lie and tell the real story behind your truths.
1. ๐ฟ Building Our Magical World#
Duration: 35 minutes
Before we can start our programming adventure, we need to set up our magical coding environment! Think of this like building a castle where all our R magic will live.
๐ฐ Step 1: Create Your Coding Castle#
Duration: 10 minutes
Head over to our Posit Cloud Setup Guide and create your castle.
๐ก Important: Complete the Posit Cloud setup before continuing with todayโs activities. This is where all your magical data science adventures will start! โจ
โ๏ธ Step 2: Learning more about Posit Cloud (same as R Studio interface)#
Duration: 5 minutes
The Magic Interface
๐ Editor (top left): Your magical scroll for writing longer R spells (code).
๐ฎ Console (bottom left): Cast spells (code) here and see the magic happen instantly!
๐ณ Environment (top right): All the magical creatures (variables, data) you create will live here.
๐บ๏ธ Files & Plots (bottom right): Find your project files and see the beautiful plots/graphs you create.

๐จ๏ธ Step 3: Practice Print Statement#
Duration: 10 minutes
๐ Activity: Magical Print Adventure!#
Time to practice your first spell - the print() command! This magical spell makes words appear on your screen.
How to cast the spell:
In the Console (bottom left), type:
print("Write down what you want to say!")Press Enter and watch the magic happen! โจ
Some print ideas:
print("My name is [YOUR NAME]")print("๐ฆฆ Oda the Data Otter says hi!")print("Fun fact: Otters hold hands when they sleep!")print("โจ I can make the computer talk to me! โจ")print("๐ This is my first day as a data scientist! ๐")
๐ก Pro Tips:
Donโt forget the quotation marks โ โ around your text!
You can use emojis to make your messages more fun
Each print statement creates a new line
๐ Common Mistake:
Forgetting the quotation marks - R needs them to know itโs text!
Forgetting the right parenthesis - R needs () both, like minions has 2 arms, it needs 2 arms to hug your sentence!
๐ Step 4: Write Your First Magical scroll โจ#
Duration: 10 minutes
Ready to write your first piece of R code? We call them scripts, but think of them as magical scrolls!
Create Your Scroll
In the top-menu, click File -> New File -> R Script.
A blank page will appear. This is where youโll write your spell!
Save Your Scroll
Click the blue save icon (๐พ).
Name your file
my_first_spell.R(or any other name you like) and click Save.
Write Your Spell
Copy and paste the code below into your new scroll:
# set variable a to have the value of 1
a <- 1
# set variable b to have the value of 2
b <- 2
# set variable c to have the value of (a + b)
c <- a + b
# print out the value of c
print(c)
Cast Your Spell!
There are a few ways to run your code:
Run one line at a time:
Click on the line of code you want to run.
Click the Run button, or press
Ctrl+Enter(Windows) orCmd+Enter(Mac).This is great for testing your spell line-by-line!
Run a chunk of code:
Highlight the lines you want to run with your mouse.
Click the Run button at the top-right of your script editor.
You will see the result in the Console below.
Run the whole script (Source):
Click the Source button to run everything in your file at once. Itโs like casting a big, powerful spell!
You should see the number
3appear in the console. You just did magic with code!
2. ๐งโโ๏ธ Understanding Your First Magical Scroll#
Duration: 35 minutes
Great job creasting your first magical scroll! But what exactly happened? Letโs break down the magic step by step.
2.2 ๐ท๏ธ Variables: Magical Storage Boxes#
Duration: 5 minutes
The real magic happens with variables. Think of variables as magical storage boxes with labels:
a <- 1 # Put the number 1 in a box labeled "a"
b <- 2 # Put the number 2 in a box labeled "b"
c <- a + b # Take what's in box "a" and "b", add them, put result in box "c"
The <- symbol is like an arrow pointing into the box. It means โstore this value here.โ
2.3 ๐ท๏ธ Variable Naming: Like Naming Your Pet!#
Duration: 5 minutes
In everyday life, we give names to complicated things to make talking easier. Instead of saying โthat furry animal with 4 legs that barks and wags its tail,โ we just say โdog!โ
In R, we do the same thing with data - we give it names so we can use it easily later.
result <- 3*2 + 5
result
# [1] 11
When R runs this code, it first calculates 3*2 + 5 (which equals 11), then stores that number in a box labeled result.
๐ก Variable Naming Rules:
Can use letters (A-Z, a-z), numbers, underscores (_), and dots (.)
Cannot start with a number** (like
2catsโ)Cannot have spaces (use
my_catnotmy catโ)Case matters -
Resultandresultare different!
๐ก Good naming style (like good pet names!):
Use lowercase letters:
my_ageโUse underscores for spaces:
favorite_colorโMake names meaningful:
student_countโ (not justa, orbโ)
2.4 ๐ Data Types: The Four Magical Islands#
Duration: 15 minutes
Just like Odaโs ocean has different islands ๐๏ธ, R has different types of data. Here are 4 data types that Oda commonly use in her otterly beautiful data adventures :
๐ข Integer Island - Whole numbers (1, 5, 100)
๐งฎ Numeric Island - Decimal numbers (1.5, 3.14, 2.7)
๐ Character Island - Words and letters (โhelloโ, โOdaโ, โletโs go swim!โ)
โ Logical Island - True or False (TRUE, FALSE)
๐ Activity: Data Islands Adventure!#
Time to explore the data islands! Weโll divide the room in half and create a magical data world.
Setup:
Half the room = Variable Boxes
students get empty boxes
โผ๏ธ TODO: Students please write your own variable names on the box
Half the room = Values (will get cards after forming islands)
Round 1: Form the Data Islands#
Value students form four islands in 4 groups:
Integer Island (whole numbers)
Numeric Island (decimal numbers)
Character Island (words)
Logical Island (TRUE/FALSE)
Instructor gives out blank value cards to each island
โผ๏ธ TODO: Students please write your own values based on your island type:
Integer Island: whole numbers (e.g., 1, 5, 10, 25)
Numeric Island: decimal numbers (e.g., 1.5, 3.14, 2.7, 5.8)
Character Island: words (e.g., โOdaโ, โmagicโ, โotterโ, โdataโ)
Logical Island: TRUE or FALSE
Round 2: Variable Assignment#
Variable students and Value students voluntarily match up (one-to-one pairs)
Each pair links arms and practices saying: โI am [variable name] and I store [value]!โ
Make sure everyone has a partner before moving on!
Round 3: Magical Calculations#
Instructor holds a calculation box labeled
resultSimple addition: โAdd up all students from Integer Island!โ
Variable + Value pairs come together and hold hands
Instructor gets the final answer and becomes e.g.,
result <- 41
Round 4: Variable Updates#
Update a variable: โbox_1, please throw away your old value, you now store 100.โ
Repeat the calculation - different result!
Important lesson: Variables can change, calculations give new results!
Round 5: Order of Operations Magic#
Big calculation:
result <- 4 + 3*4Show the steps:
First:
3*4students hold hands โ make 12Then:
4 + 12โ make 16Finally: assign 16 to
result
Key lesson: Multiplication happens before addition, before value assignment to variables!
2.5 ๐ฅ Errors#
Duration: 5 minutes
๐ Donโt worry - Everyone makes mistakes when coding!
R is like learning a new language! Just like human languages, R has rules. But R is different from human languages in two important ways:
The rules are simple - You can learn most of them in just a few weeks!
The rules are strict - Unlike humans who can understand you even with small mistakes, computers need you to follow the rules exactly.
Even Oda the Otter makes mistakes when casting data spells! When you forget something in your code, R will show you an error message to help you fix it.
๐ Activity: Letโs try making a mistake on purpose:#
In the Console, try typing this broken spell:
print("My favorite number is " + 1)
What happened?
R shows you an error message like โnon-numeric argument to binary operatorโ
This means you canโt add text (โMy favorite number is โ) and numbers (1) together - theyโre different types!
How to fix it:
print("My favorite number is 1") # โจ Now it works!
๐ก Common Beginner Mistakes:
Forgetting the closing
)parenthesisForgetting quotation marks
"Mixing up different data types
๐ฏ The Most Important Skill: Debugging! Learning to find problems, fix them, and move on is one of the MOST important skill in coding. Even your instructor makes errors every day!
This skill will make you a coding superhero! ๐ฆธโโ๏ธ๐ฆธโโ๏ธ
๐คก Fun Fact: Why are errors in code called bug?#
Because it was actually a BUGโฆ
In 1947, computer scientist Grace Hopper found an actual moth stuck in a the powerful computer Mark II at Harvard, which caused the computer to break down! She taped it in her logbook and wrote โFirst actual case of bug being found.โ Since then, we call computer problems โbugsโ and fixing them โdebuggingโ! ๐
๐ก Pro Tips:#
๐พ Save your work often! Click the save icon or press
Ctrl+S(Windows) /Cmd+S(Mac).๐ Errors are okay! They are just clues to help you learn and fix your spell.
๐ Make your code colorful! Rainbow parentheses help you see where code blocks start and end. Hereโs how to turn them on:
Go to Tools -> Global Options.
Click Code on the left.
Go to the Display tab on the top.
Check the box for Use rainbow parentheses.
Click Apply!
๐ Name things like a wizard! Good names help you remember what your magical creatures (variables) do:
Use
snake_caseorkabab-case(likemy_ageorfavorite-color)Always start with a letter, never a number (โ
age1โ1age)Only use lowercase letters, numbers, and
-or_Make names short but meaningful (this is hard but important!)
๐ Troubleshooting#
โobject โmy_variableโ not foundโ#
What it means: You tried to use a magical creature (variable) that R doesnโt know about.
Why it happens:
You forgot to create your variable first (like
my_variable <- 10)You have a typo in the name
The Fix: Run the line that creates your variable first, then check your spelling!
Case-Sensitivity Chaos#
What it means: R thinks
MyDataandmydataare totally different things.Why it happens: R is picky about capital letters.
The Fix: Always spell your variables exactly the same way - capitals matter!
Missing Parentheses or Quotes: ( or โ#
What it means: You opened something but forgot to close it.
Why it happens: Easy to forget!
The Fix: Count your
(and), and your"marks. The rainbow parentheses we turned on help you see which ones match!
3. ๐ช Making Magical Decisions: If-Else Adventures#
Duration: 95 minutes
Welcome to the next magical part of programming - teaching your code to make decisions! Just like Oda the Otter decides whether to swim upstream or downstream based on the current, our code can make choices too! ๐ฆฆโจ
3.1 ๐ Activity: The Magic Color Decision Game#
Duration: 15 minutes
Before we write code, letโs experience how decision-making works for humans using our voice and body!
How to Play:#
Look at your shirt color
Follow Odaโs Magic Rules:
Round 1: Simple If-Else#
IF youโre wearing WHITE โ โRoar like a lion!โ ๐ฆ
ELSE โ โSwim like an otter!โ ๐ฆฆ
# ๐ฎ Simple if-else decision
shirt_color <- "white" # Try changing to other colors!
if (shirt_color == "white") {
print("๐ฆ Roar like a lion!")
} else {
print("๐ฆฆ Swim like an otter!")
}
Round 2: OR condition#
IF youโre wearing WHITE or BLUE โ โRoar like a lion!โ ๐ฆ
ELSE โ โSwim like an otter!โ ๐ฆฆ
# ๐ฎ OR condition decision
shirt_color <- "blue" # Try "white", "blue", or "red"
if (shirt_color == "white" | shirt_color == "blue") {
print("๐ฆ Roar like a lion!")
} else {
print("๐ฆฆ Swim like an otter!")
}
Round 3: AND condition#
IF youโre wearing WHITE and BLUE โ โRoar like a lion!โ ๐ฆ
ELSE โ โSwim like an otter!โ ๐ฆฆ
# ๐ฎ AND condition decision (need BOTH colors)
shirt_color <- "white"
pants_color <- "blue"
if (shirt_color == "white" & pants_color == "blue") {
print("๐ฆ Roar like a lion!")
} else {
print("๐ฆฆ Swim like an otter!")
}
Round 4: If-ElseIf-Else#
IF youโre wearing BLACK โ โBark like a dog!โ ๐ถ
ELSE IF youโre wearing BLUE โ โHop like a bunny!โ ๐ฐ
ELSE โ โMoo like a cow!โ ๐ฎ
# ๐ฎ Multiple condition decision
shirt_color <- "black" # Try "black", "blue", or "red"
if (shirt_color == "black") {
print("๐ถ Bark like a dog!")
} else if (shirt_color == "blue") {
print("๐ฐ Hop like a bunny!")
} else {
print("๐ฎ Moo like a cow!")
}
Round 5: No Else (Some people might not do anything!)#
IF youโre wearing BLACK โ โBark like a dog!โ ๐ถ
ELSE IF youโre wearing BLUE โ โHop like a bunny!โ ๐ฐ
# ๐ฎ No "else" - some conditions don't trigger any action
shirt_color <- "red" # Try "black", "blue", or "red"
if (shirt_color == "black") {
print("๐ถ Bark like a dog!")
} else if (shirt_color == "blue") {
print("๐ฐ Hop like a bunny!")
}
# Notice: No "else" - if wearing red, nothing happens!
Round 6: Multiple actions#
IF youโre wearing BLACK โ Stand up โ โBark like a dog!โ ๐ถ
ELSE IF youโre wearing BLUE โ Hands in the air โ โHop like a bunny!โ ๐ฐ
ELSE โ cover your eyes โ โMoo like a cow!โ ๐ฎ
# ๐ฎ Multiple actions in each condition
shirt_color <- "black" # Try different colors
if (shirt_color == "black") {
print("๐ง First, I stand up!")
print("๐ถ Then I bark like a dog!")
} else if (shirt_color == "blue") {
print("๐ First, hands in the air!")
print("๐ฐ Then hop like a bunny!")
} else {
print("๐ First, I cover my eyes!")
print("๐ฎ Then moo like a cow!")
}
๐ก What Did We Learn?#
Decisions follow a pattern: Check a condition โ Do 1 or more action(s)
We can combine conditions with OR (
|) and AND (&)We can have multiple conditions with โelse ifโ
We can have a backup plan with โelseโ - but itโs optional!
We can do multiple actions inside each condition
Only ONE path gets chosen - just like only one group of people did each action!
3.2 ๐ฅ๏ธ Coding Magic: Teaching R to Make Decisions#
Duration: 40 minutes
Now letโs teach R the same decision-making magic! In your R script, try these spells:
โจ Spell 1: Simple If-Else Magic#
๐ Find this spell in Posit Cloud: Look for the file day01_spell01_simple_if_else.R in your project files!
# ๐ฎ Oda's first decision spell
my_favorite_number <- 7
if (my_favorite_number > 5) {
print("๐ Wow! That's a big number!")
} else {
print("๐ญ That's a nice small number!")
}
๐ Activity: Try It Yourself!#
Change
my_favorite_numberto different values (try 3, 10, 1)Run the code each time - what happens?
Challenge: Can you predict the message before running it?
โจ Spell 2: Multiple Choices (If-ElseIf-Else)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell02_multiple_choices.R in your project files!
# ๐ Oda's color mood detector
favorite_color <- "blue"
if (favorite_color == "red") {
print("๐ฅ You like bold and exciting adventures!")
} else if (favorite_color == "blue") {
print("๐ You love calm and peaceful vibes!")
} else if (favorite_color == "green") {
print("๐ฑ You enjoy nature and growing things!")
} else {
print("โจ You have a unique and magical taste!")
}
๐ Activity: Customize Your Color Magic!#
Change
favorite_colorto your actual favorite colorIf your color isnโt there, what message do you get?
Extension: Add your favorite color with a special message!
โจ Spell 3: Number Range Detective#
๐ Find this spell in Posit Cloud: Look for the file day01_spell03_number_range.R in your project files!
# ๐ Oda's number detective game
mystery_number <- 4
if (mystery_number < 10) {
print("๐ฃ Tiny number - like a baby otter!")
} else if (mystery_number < 50) {
print("๐ฆฆ Medium number - like a young otter!")
} else if (mystery_number < 100) {
print("๐๏ธ Big number - like a mountain otter!")
} else {
print("๐ Huge number - like a space otter!")
}
๐ Activity: Number Range Challenge!#
Try these mystery numbers: 5, 25, 75, 150
Predict first: Which otter will you get for each number?
Challenge: What happens if you use 0 or negative numbers?

โจ Spell 4: AND Condition Magic (Both Things Must Be True!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell04_and_condition.R in your project files!
# ๐ญ Oda's perfect day detector
temperature <- 25
weather <- "sunny"
if (temperature > 20 & weather == "sunny") {
print("๐๏ธ Perfect day for swimming and playing!")
print("๐ฆฆ Oda is super happy!")
} else {
print("๐ Maybe it's a good day to stay inside and code!")
}
๐ Activity: Weather Wizard!#
Try these combinations and see what Oda does:
temperature <- 15andweather <- "sunny"temperature <- 25andweather <- "rainy"temperature <- 30andweather <- "sunny"
โจ Spell 5: OR Condition Magic (Either Thing Can Be True!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell05_or_condition.R in your project files!
# ๐ Oda's snack time detector
snack <- "apple"
if (snack == "apple" | snack == "banana" | snack == "carrot") {
print("๐ฅฐ Yummy! Oda loves healthy snacks!")
print("๐ช This will give me energy for coding!")
} else if (snack == "cookie" | snack == "candy") {
print("๐ช Sweet treat! But just a little bit!")
} else {
print("๐ค Hmm, Oda isn't sure about this snack...")
}
๐ Activity: Snack Sorter!#
Test with these snacks: โappleโ, โcookieโ, โpizzaโ, โbananaโ
Extension: Add your favorite snack to one of the categories!
Question: What happens if you type โAppleโ with a capital A?
โจ Spell 6: Age Group Sorter (Like the Physical Activity!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell06_age_groups.R in your project files!
# ๐ Oda's age group magic (just like our shirt color game!)
age <- 4
if (age < 6) {
print("๐ฃ Little explorer - just starting the adventure!")
} else if (age >= 6 & age <= 9) {
print("๐งธ Young adventurer - ready for fun!")
} else if (age >= 10 & age <= 14) {
print("๐งโโ๏ธ Data wizard in training - that's you!")
} else if (age >= 15 & age <= 18) {
print("๐ Teen tech master - almost ready to rule the world!")
} else {
print("๐ Wise adult - teaching the next generation!")
}
๐ Activity: Age Detective Challenge!#
Try your real age and see what message you get!
Try ages: 5, 8, 12, 16, 25
Family Fun: Try your family membersโ ages!
โจ Spell 7: Multiple Actions Spell (Do Several Things!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell07_multiple_actions.R in your project files!
# ๐ช Oda's magic show with multiple tricks
magic_word <- "abracadabra"
if (magic_word == "abracadabra") {
print("โจ *Sparkles appear in the air*")
print("๐ฉ *A rabbit pops out of the hat*")
print("๐ *The crowd cheers loudly*")
print("๐ฆฆ Oda takes a bow!")
} else if (magic_word == "alakazam") {
print("๐ฅ *Thunder sound effect*")
print("๐ *Colorful fireworks explode*")
print("๐ฆฆ Oda does a backflip!")
} else {
print("๐
Oops! That's not a magic word Oda knows...")
print("๐ก Try 'abracadabra' or 'alakazam'!")
}
๐ Activity: Build Your Own Magic Show!#
Try both magic words and see the different shows!
Creative Challenge: Add a third magic word with your own magic show!
Question: Why do we use multiple
print()statements?

โจ Spell 8: No Else Challenge (Sometimes Nothing Happens!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell08_no_else.R in your project files!
# ๐ Oda's achievement unlocked detector
points <- 85
if (points >= 100) {
print("๐ LEGENDARY! You're a coding master!")
}
if (points >= 75) {
print("โญ AWESOME! You're doing great!")
}
if (points >= 50) {
print("๐ GOOD JOB! Keep practicing!")
}
# Notice: No "else" - you might get multiple messages!
print(paste("๐ฏ You have", points, "points total!"))
๐ Activity: Achievement Hunter!#
Try these point values: 30, 60, 80, 120
Question: Why might someone get multiple achievement messages?
Challenge: Whatโs the minimum points to get all three achievements?
3.3 ๐ฏ The Magic of Code Order!#
Duration: 15 minutes
๐ก Why Sequence Matters in Magic Spells!#
Just like Oda must perform her magic spells in the right order, the sequence of code matters! Letโs discover why with some magical experiments!
โจ Spell 9A: Order Matters - The Racing Game!#
๐ Find this spell in Posit Cloud: Look for the file day01_spell09A_code_order.R in your project files!
# ๐ Oda's magical racing game - Position matters!
position <- 2
# Version 1: Check from FIRST place to LAST place
if (position == 1) {
print("๐ฅ GOLD MEDAL! You're the champion!")
} else if (position == 2) {
print("๐ฅ SILVER MEDAL! Amazing job!")
} else if (position == 3) {
print("๐ฅ BRONZE MEDAL! Great effort!")
} else {
print("๐ Good race! Keep practicing!")
}
โจ Spell 9B: Wrong Order - The Broken Magic!#
๐ Find this spell in Posit Cloud: Look for the file day01_spell09B_code_order.R in your project files!
# โ ๏ธ What happens if we check from LAST place to FIRST place?
position <- 2
# Version 2: This might not work as expected!
if (position >= 1) {
print("๐ Good race! Keep practicing!")
} else if (position == 1) {
print("๐ฅ GOLD MEDAL! You're the champion!")
} else if (position == 2) {
print("๐ฅ SILVER MEDAL! Amazing job!")
}
๐ Activity: Code Detective!#
Try both versions with position = 1, 2, and 3
Question: Why does Version 2 always say โGood race!โ even for 1st place?
Discovery: Which conditions will NEVER run in Version 2 and why?
โจ Spell 10: String Detective (Text Comparison Magic!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell10_string_detective.R in your project files!
# ๐ Oda is trying to find a old friend of her, whose name is Mike
name <- "Mike"
if (nchar(name) <= 4){
print("๐ You have a nice short name!")
} else if (name == "mike" | name == "Mike" | name == "Michael" | name == "michael"){
print("๐ฅ Hi Mike, long time no see! I've caught some clams today! ๐ ๐ ๐ฆช")
} else if (nchar(name) >= 8) {
print("๐ Wow! You have a beautifully long name!")
} else {
print("โจ You have a perfectly medium-sized name!")
}
# Bonus: Count the letters in your name!
print(paste("๐ข Your name has", nchar(name), "letters!"))
๐ Activity: Name Explorer!#
Try your real name and see what category you get!
Try these names: โJoโ, โElizabethโ, โSamโ, โChristopherโ
Question: What does
nchar()do? Try it with different words!Challenge: Why canโt Oda ever find Mike? Can you fix this code so that Oda can find Mike?

๐ Congratulations! Youโre a Decision-Making Wizard!#
โจ Creative Extension Ideas:#
Create a โWhat should I wear today?โ decision tree using temperature ranges
Build a โWhatโs my spirit animal?โ quiz using age ranges
Make a โHow many hours of sleep do I need?โ advisor using age groups
Design a โWhat sport should I try?โ recommender using height ranges
3.4 ๐ง Understanding Decision Logic#
Duration: 5 minutes
The Magical Comparison and Condition Symbols:#
==means โis exactly equal toโ (like twin otters! ๐ฆฆ == ๐ฆฆ)>means โis greater thanโ (elephant is larger than otter ๐ > ๐ฆฆ)<means โis less thanโ (mouse is smaller than otter! ๐ญ < ๐ฆฆ)>=means โgreater than or equal toโ<=means โless than or equal toโ!=means โis NOT equal toโ (otters are different from birds! ๐ฆฆ != ๐ง)|means โorโ (I would like to meet Mr. Fish or Mrs. Oda today: ๐ or ๐ฆฆ)&means โandโ (I like otter and elephant ๐ & ๐ฆฆ)
โจ Spell 11: Temperature Zone Detective (Real World >= <= Examples!)#
๐ Find this spell in Posit Cloud: Look for the file day01_spell11_temperature_zones.R in your project files!
# ๐ก๏ธ Oda's temperature zone detector
temperature <- 22 # In Celsius
if (temperature <= 0) {
print("๐ง FREEZING! Time for hot cocoa and warm blankets!")
print("๐ฆฆ Oda is wearing a thick winter coat!")
} else if (temperature >= 1 & temperature <= 10) {
print("๐ฅถ COLD! Perfect sweater weather!")
} else if (temperature >= 11 & temperature <= 20) {
print("๐ COOL! Great for a light jacket!")
} else if (temperature >= 21 & temperature <= 30) {
print("โ๏ธ WARM! Perfect for playing outside!")
} else if (temperature >= 31) {
print("๐ฅ HOT! Time for swimming and ice cream!")
print("๐ฆฆ Oda jumps in the river to cool off!")
} else {
print("๐ค Something seems fishy with this temperature...")
}
๐ Activity: Weather Station!#
Try these temperatures: -5, 5, 15, 25, 35
Real World: Check todayโs actual temperature and see what Oda says!
Question: Why do we use
>=and<=instead of just>and<?Errors: What input would cause R to generate errors?
3.5 ๐ฎ Challenge: Build Your Own Decision Tree!#
Duration: 15 minutes
Create a magical creature classifier! Copy this code and fill in the blanks:
โจ Spell 12: Magical creature detector!#
๐ Find this spell in Posit Cloud: Look for the file day01_spell12_magical_creatures.R in your project files!
# ๐ฆ Magical Creature Classifier
creature_size <- 50 # Size in cm - Try: 10, 50, 200
creature_color <- "purple" # Try: "purple", "gold", "rainbow"
if (creature_size <= 20) {
if (creature_color == "purple") {
print("๐งโโ๏ธ You found a tiny fairy!")
} else {
print("๐ You found a magical bug!")
}
} else if (creature_size >= 21 & creature_size <= 100) {
if (creature_color == "purple") {
print("๐ฆ You found a unicorn!")
} else if (creature_color == "gold") {
print("๐ You found a baby dragon!")
} else {
print("๐ฆฆ You found Oda the Otter!")
}
} else if (creature_size >= 101) {
print("๐น You found a friendly giant!")
}
๐ Activity: Create your own creature:**#
Try different combinations of
creature_size(15, 50, 150) andcreature_colorAdd a new size range (maybe 101-300 for โlargeโ creatures) with your own magical creature
Add a new color option with your own special message
Challenge: What happens if you use negative numbers or zero for size?
๐ Activity: Have your friends try to find creatures in your code!#

3.6 ๐ Common Mistakes & How to Fix Them#
Duration: 5 minutes
Mistake 1: Using = instead of ==
# โ Wrong - this assigns a value
if (x = 5) { ... }
# โ
Correct - this compares values
if (x == 5) { ... }
Mistake 2: Forgetting curly braces
# โ Hard to read
if (x > 5) print("big")
# โ
Clear and safe
if (x > 5) {
print("big")
}
Mistake 3: Missing parentheses
# โ R gets confused
if x > 5 { ... }
# โ
R understands perfectly
if (x > 5) { ... }
๐ก Pro Tips:
Test your conditions with simple values first
Use meaningful variable names:
student_agenot justxUse
==for comparison, not==assigns a value (like<-)==checks if two things are the same
Conditions go in parentheses:
if (condition)Actions go in curly braces:
{ action }Only ONE path gets chosen - just like our physical game!
๐ Congratulations! Youโve learned how to make your code think and decide! This is one of the most powerful tools in programming - you can now create interactive programs that respond differently based on input!

4. ๐ Pro Tips Cheatsheet#
Here are all the helpful tricks and important knowledge you learned today to become a coding wizard! ๐งโโ๏ธ
๐พ Saving Your Work#
Save often! Click the save icon or press
Ctrl+S(Windows) /Cmd+S(Mac)
๐ Writing Clean Code#
Always use quotation marks
" "around textEach print statement creates a new line
Add comments to explain your own logic to others and your future self
Use the
#symbol to make comments - R ignores everything after it
๐ฆ Understanding Variables (Magical Storage Boxes)#
Variables are like labeled storage boxes that hold your data
The
<-symbol is like an arrow pointing into the box (variable) that stores this value:my_age <- 10You can change whatโs in the box anytime:
my_age_next_year <- 11Variables make calculations easier:
my_age_next_year <- my_age + 1instead of remembering numbers
๐ท๏ธ Naming Variables Like a Wizard#
Use
snake_case(lower case characters separated by_, likemy_ageorfavorite_color)Always start with a letter, never a number (โ
age1โ1age)Only use lowercase letters, numbers, and
_or-Make names short but meaningful
Use meaningful names:
student_agenota, orbCase matters!
Resultandresultare different things to R
๐ The Four Basic Data Type Islands#
๐ข Integer - Whole numbers (1, 5, 100)
๐งฎ Numeric - Decimal numbers (1.5, 3.14, 2.7)
๐ Character - Words and letters (โhelloโ, โOdaโ, โletโs go swim!โ)
โ Logical - True or False (TRUE, FALSE)
โก Code Order Magic - Sequence Matters!#
Important: The order of your code, like the sequence of if-else conditions matters!
R checks conditions from top to bottom and stops at the first TRUE one
Always check specific conditions first, then general ones
Example: Check
if (x == 1)beforeif (x >= 1)or the second one will never run!Think like Odaโs magic spells - they must be cast in the right order! โจ
๐ Making Code Pretty#
Turn on rainbow parentheses to see code blocks better:
Go to Tools โ Global Options
Click Code โ Display tab
Check Use rainbow parentheses
Click Apply!
๐ Decision Making Magic (Comparison Symbols)#
==means โis exactly equal toโ (like twin otters! ๐ฆฆ == ๐ฆฆ)Use
==for comparison, not==assigns a value (like<-)
>means โis greater thanโ (elephant is larger than otter ๐ > ๐ฆฆ)<means โis less thanโ (mouse is smaller than otter! ๐ญ < ๐ฆฆ)>=means โgreater than or equal toโ<=means โless than or equal toโ!=means โis NOT equal toโ (otters are different from birds! ๐ฆฆ != ๐ง)|means โorโ (I would like to meet Mr. Fish or Mrs. Oda today: ๐ or ๐ฆฆ)&means โandโ (I like otter and elephant ๐ & ๐ฆฆ)
๐ฏ If-Else Structure Rules#
Put conditions in parentheses:
if (condition)Put actions in curly braces:
{ action }Test your conditions with simple values first
Remember: Only ONE path gets chosen in your if-else condition cluster!
You can have multiple actions in each condition block
โelseโ is optional - sometimes you only want something to happen under certain conditions
๐ข Order of Operations (Math Rules)#
R follows math rules: multiplication and division happen before addition and subtraction
Example:
4 + 3*4=4 + 12=16(not7*4=28)Use parentheses to change the order:
(4 + 3)*4=7*4=28
๐ Dealing with Errors#
Errors are okay! Theyโre just clues to help you learn. Even teachers make errors every day!
Debugging is one of the most important coding skills
Try to understand where your bug comes from โ fix it โ move on!
Run your code line by line to find where problems start
5. ๐ Troubleshooting Cheatsheet#
When things go wrong, hereโs how to fix them! ๐ง
๐ค Text and Print Problems#
Problem: Forgetting quotation marks
Fix: Put
" "around all textExample: โ
print("Hello!"), โprint(Hello!)
Problem: Forgetting closing parenthesis
Fix: Count your
(and)- they need to match!Example: โ
print("Hi!"), โprint("Hi!"
Problem: Mixing text and numbers wrong
Fix: Donโt try to add text + numbers
โ :
print("My age is 5")โ:
print("My age is " + 5)
๐ท๏ธ Variable Problems#
Problem: โobject โmy_variableโ not foundโ
What it means: You tried to use a variable R doesnโt know about
Fix: Create your variable first:
my_variable <- 10Fix: Check your spelling - did you type it exactly the same?
Problem: Case-sensitivity chaos
What it means: R thinks
MyDataandmydataare differentFix: Always spell variables exactly the same way - capitals matter!
๐ค Decision Making Problems#
Problem: Using = instead of ==
โ :
if (x == 5)- this compares valuesโ:
if (x = 5)- this tries to assign a value
Problem: Forgetting curly braces
โ Hard to read:
if (x > 5) print("big")โ Better:
if (x > 5) { print("big") }
Problem: Missing parentheses around conditions
โ :
if (x > 5) { ... }โ :
if x > 5 { ... }
๐ง Quick Fixes#
Problem: Missing quotes or parentheses
Fix: Count your
"marks and()- they need to matchTip: Rainbow parentheses help you see which ones go together!
Problem: Code doesnโt work as expected
Fix: Run your code line by line to find where it breaks
Fix: Check that you spelled everything exactly the same way
๐ก Remember#
Making mistakes is how we learn!
When stuck, check: spelling, quotation marks, and parentheses
Ask for help - coding is more fun together! ๐ฆฆโจ
Remember: Every expert programmer started exactly where you are today. Have fun, be curious, and donโt be afraid to make mistakes - thatโs how we learn! โจ
2.1 ๐ฌ Comments: Messages to Future You#
Duration: 5 minutes
First, notice the lines that start with
#. These are called comments:# set variable a to have the value of 1๐ก What are comments?
Comments are notes you write to yourself (and others) to explain what your code does
The computer ignores everything after the
#symbolThink of them as sticky notes on your magical scrolls!
๐ค Why use comments?
They help you remember what you were thinking
They help others understand your magic
They make finding and fixing mistakes easier!
๐ Try this:
Add a comment to your spell that says what the final answer should be!
Add a comment at the begining of the magical scroll to sign your name:
# This magical scroll belongs to: YOUR NAME