Skip to content

Exercise 2 of 10 · Variables

Greet a Learner by Name

What you will make

A one-line personalized greeting built from a value stored in a variable.

The one new idea: Assigning values with <-

Nearly every R program stores values first and uses them later — getting variable names exactly right, every single time you reference them, is a habit you'll rely on in every exercise from here on.

Go straight to the code ↓

The concept

In R, you store a value using the assignment arrow <-. Writing name <- "Meera" creates a variable called name that holds the text "Meera". From that point on, anywhere later in the script that you write name, R substitutes in the stored value. This is how programs remember information and reuse it instead of retyping it everywhere.

The catch is that R matches variable names exactly. It doesn't do fuzzy matching or autocorrect — name and nam are two completely different, unrelated identifiers as far as R is concerned. If you reference a name that was never assigned, R has nothing to substitute and raises an error rather than silently printing something blank.

Worked example

r
city <- "Nairobi"
cat("Welcome to ", city, "!\n", sep = "")
Output
Welcome to Nairobi!

Here, city is defined once and then reused inside cat(). Because the spelling matches exactly — same letters, same case — R finds the stored value without any trouble and slots it into the message. Notice also sep = "": without it, cat() would insert its own space between each argument, turning the tidy "Welcome to Nairobi!" into something with extra, unwanted spaces.

Your turn

The starter code defines a variable called name but then tries to print something called nam — one letter short. Find the line where the shorter, misspelled version appears and correct it so it matches the variable's actual name exactly, letter for letter.

If something goes wrong

If running the code produces an error mentioning an object that "was not found," that confirms the diagnosis: some line refers to a variable that was never created. Scroll up to the line that uses <- to see the real spelling, then search the rest of the code for anywhere that name is written even slightly differently — a missing letter, an extra letter, or different capitalization all count as a different name to R.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Assuming R will figure out which variable you meant.
R never guesses — a name that doesn't exactly match an existing variable is always a hard error ("object not found"), even if it's off by a single letter.
Renaming the original variable to match the typo instead of fixing the typo.
That would technically run, but it means changing correct code to match a mistake instead of fixing the actual mistake — and it breaks the habit of trusting your original, deliberate names.
Removing sep = "" while fixing the typo.
Without sep = "", cat() inserts its own space between every argument, turning "Hello, Meera!" into "Hello, Meera !" with extra spaces in the wrong places.

Want a blank editor instead? Open the R playground.