Module 14 ยท What are your first R commands?
Talk to R like a calculator
By the end of this module you will be able to
- Use R as a simple calculator.
- Store a value in a variable with the arrow.
- View what a variable contains.
Type this into the Console and press Enter. R answers right away, no small talk.
Console
2 + 2
Output[1] 4
That [1] simply means "this is the first, and in this case only, answer." Now let's store a number in a variable using the arrow <-, which is R's slightly dramatic way of saying "put this into."
Console
# put the number 42 into a box named "coverage" coverage <- 42 # now look inside the box coverage
Output[1] 42
Explain it like I'm ten
coverage <- 42 means "label a box coverage and drop 42 inside." Whenever you write coverage later, R opens the box and shows you what's in it. That's all a variable is, a labelled box. No magic, no exam.
Reflect
Try a couple of commands of your own.
- What happens if you store your district's coverage in a variable and call it back?
- Does the idea of a 'labelled box' make variables feel less mysterious?