๐Ÿฐ Day 1: Basic Programming Magic in R

Contents

๐Ÿฐ Day 1: Basic Programming Magic in R#

Welcome to the Magic Castle where Oda the Otter teaches you the secret spells of programming!

logo2

๐Ÿ”ฎ 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!#

Hello GIF

๐ŸŽจ Two Truths and a Creative Lie#

Duration: 10 minutes

How to Play:

  1. For small groups of 3-4 people. Try to sit with people you donโ€™t know very well!

  2. 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)

  3. 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โ€

  4. Guess the lie! After each person shares, everyone else tries to figure out which one is the creative lie.

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

R Studio Interface

๐Ÿ–จ๏ธ 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:

  1. In the Console (bottom left), type: print("Write down what you want to say!")

  2. 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!

minion2arm

๐Ÿ“œ 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!

  1. 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!

  1. Save Your Scroll

  • Click the blue save icon (๐Ÿ’พ).

  • Name your file my_first_spell.R (or any other name you like) and click Save.

  1. 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)
  1. 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) or Cmd+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 3 appear in the console. You just did magic with code!

celebrate

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.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 # symbol

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

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_cat not my cat โŒ)

  • Case matters - Result and result are 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 just a, or b โŒ)

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 :

  1. ๐Ÿ”ข Integer Island - Whole numbers (1, 5, 100)

  2. ๐Ÿงฎ Numeric Island - Decimal numbers (1.5, 3.14, 2.7)

  3. ๐Ÿ“ Character Island - Words and letters (โ€œhelloโ€, โ€œOdaโ€, โ€œletโ€™s go swim!โ€)

  4. โœ… Logical Island - True or False (TRUE, FALSE)

minion2arm

๐ŸŽˆ 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#
  1. Value students form four islands in 4 groups:

    • Integer Island (whole numbers)

    • Numeric Island (decimal numbers)

    • Character Island (words)

    • Logical Island (TRUE/FALSE)

  2. Instructor gives out blank value cards to each island

  3. โ€ผ๏ธ 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#
  1. Variable students and Value students voluntarily match up (one-to-one pairs)

  2. Each pair links arms and practices saying: โ€œI am [variable name] and I store [value]!โ€

  3. Make sure everyone has a partner before moving on!

Round 3: Magical Calculations#
  1. Instructor holds a calculation box labeled result

  2. Simple addition: โ€œAdd up all students from Integer Island!โ€

  3. Variable + Value pairs come together and hold hands

  4. Instructor gets the final answer and becomes e.g., result <- 41

Round 4: Variable Updates#
  1. Update a variable: โ€œbox_1, please throw away your old value, you now store 100.โ€

  2. Repeat the calculation - different result!

  3. Important lesson: Variables can change, calculations give new results!

Round 5: Order of Operations Magic#
  1. Big calculation: result <- 4 + 3*4

  2. Show the steps:

    • First: 3*4 students hold hands โ†’ make 12

    • Then: 4 + 12 โ†’ make 16

    • Finally: assign 16 to result

  3. 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!

error

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:

  1. The rules are simple - You can learn most of them in just a few weeks!

  2. 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 ) parenthesis

  • Forgetting 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โ€! ๐Ÿ›

bug

๐Ÿ’ก 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:

    1. Go to Tools -> Global Options.

    2. Click Code on the left.

    3. Go to the Display tab on the top.

    4. Check the box for Use rainbow parentheses.

    5. Click Apply!

  • ๐Ÿ“ Name things like a wizard! Good names help you remember what your magical creatures (variables) do:

    • Use snake_case or kabab-case (like my_age or favorite-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 MyData and mydata are 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:#

  1. Look at your shirt color

  2. 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!#
  1. Change my_favorite_number to different values (try 3, 10, 1)

  2. Run the code each time - what happens?

  3. 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!#
  1. Change favorite_color to your actual favorite color

  2. If your color isnโ€™t there, what message do you get?

  3. 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!#
  1. Try these mystery numbers: 5, 25, 75, 150

  2. Predict first: Which otter will you get for each number?

  3. Challenge: What happens if you use 0 or negative numbers?

tiny

โœจ 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!#
  1. Try these combinations and see what Oda does:

    • temperature <- 15 and weather <- "sunny"

    • temperature <- 25 and weather <- "rainy"

    • temperature <- 30 and weather <- "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!#
  1. Test with these snacks: โ€œappleโ€, โ€œcookieโ€, โ€œpizzaโ€, โ€œbananaโ€

  2. Extension: Add your favorite snack to one of the categories!

  3. 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!#
  1. Try your real age and see what message you get!

  2. Try ages: 5, 8, 12, 16, 25

  3. 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!#
  1. Try both magic words and see the different shows!

  2. Creative Challenge: Add a third magic word with your own magic show!

  3. Question: Why do we use multiple print() statements?

spell

โœจ 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!#
  1. Try these point values: 30, 60, 80, 120

  2. Question: Why might someone get multiple achievement messages?

  3. Challenge: Whatโ€™s the minimum points to get all three achievements?

celebrate

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!#
  1. Try both versions with position = 1, 2, and 3

  2. Question: Why does Version 2 always say โ€œGood race!โ€ even for 1st place?

  3. 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!#
  1. Try your real name and see what category you get!

  2. Try these names: โ€œJoโ€, โ€œElizabethโ€, โ€œSamโ€, โ€œChristopherโ€

  3. Question: What does nchar() do? Try it with different words!

  4. Challenge: Why canโ€™t Oda ever find Mike? Can you fix this code so that Oda can find Mike?

what

๐ŸŽ‰ 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!#
  1. Try these temperatures: -5, 5, 15, 25, 35

  2. Real World: Check todayโ€™s actual temperature and see what Oda says!

  3. Question: Why do we use >= and <= instead of just > and <?

  4. 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:**#
  1. Try different combinations of creature_size (15, 50, 150) and creature_color

  2. Add a new size range (maybe 101-300 for โ€œlargeโ€ creatures) with your own magical creature

  3. Add a new color option with your own special message

  4. Challenge: What happens if you use negative numbers or zero for size?

๐ŸŽˆ Activity: Have your friends try to find creatures in your code!#

Code Order Magic

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_age not just x

  • Use == 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!

Code Order Magic


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 text

  • Each 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 <- 10

  • You can change whatโ€™s in the box anytime: my_age_next_year <- 11

  • Variables make calculations easier: my_age_next_year <- my_age + 1 instead of remembering numbers

๐Ÿท๏ธ Naming Variables Like a Wizard#

  • Use snake_case (lower case characters separated by _, like my_age or favorite_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_age not a, or b

  • Case matters! Result and result are different things to R

๐ŸŒŠ The Four Basic Data Type Islands#

  1. ๐Ÿ”ข Integer - Whole numbers (1, 5, 100)

  2. ๐Ÿงฎ Numeric - Decimal numbers (1.5, 3.14, 2.7)

  3. ๐Ÿ“ Character - Words and letters (โ€œhelloโ€, โ€œOdaโ€, โ€œletโ€™s go swim!โ€)

  4. โœ… 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) before if (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:

    1. Go to Tools โ†’ Global Options

    2. Click Code โ†’ Display tab

    3. Check Use rainbow parentheses

    4. 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 (not 7*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 text

  • Example: โœ… 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 <- 10

  • Fix: Check your spelling - did you type it exactly the same?

Problem: Case-sensitivity chaos

  • What it means: R thinks MyData and mydata are different

  • Fix: 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 match

  • Tip: 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! โœจ