Rithmatic
Exposition
Introduction
One of the best ways to start learning R is to use it as a calculator. Below we’ll start to do some simple arithmetic in R. We are using a special kind of R called webr
that runs directly in your web browser.
This means you can use R without having to install anything on your computer!
When you see text that is formated like 2 + 2
, it is code that should be typed exactly as it is written for R to read or represents a verbatim response from R.
Addition
Try typing 2 + 2
in the box labelled “Exercise” just below this paragraph and then click on the Run Code button in the upper right to evaluate the code. When you run the code, you should see the result [1] 4
printed below the editor. We’ll learn why there is a [1]
in front of the answer 4
later.
2 + 2
2 + 2
Subtraction
Now try subtraction below by asking R what 20 - 4.2
equals. However, this time, instead of clicking Run Code, use the keyboard shortcut Ctrl + Enter on Windows/Linux or ⌘ command + Enter on Mac (note the + sign means to press both keys at the same time).
Using the keyboard shortcut is a lot faster than clicking the button, so you should get in the habit of using it for all the exercises below.
20 - 4.2
20 - 4.2
Why stop with just one subtraction? Now try 100 - 20 - 5
.
100 - 20 - 5
100 - 20 - 5
Multiplication and Division
OK, now multiplication: 5 * 4 * 3 * 2 * 1
.
5 * 4 * 3 * 2 * 1
5 * 4 * 3 * 2 * 1
How about division with 25 / 5
?
25 / 5
25 / 5
Experimentation
Division by Zero
Now is a time for you to extend your knowledge by exploring and experimenting some yourself. So before you answer the next question you need to play around some.
Below is a code editor where you can type in any R code you want.
First, figure out what R thinks a number divided by zero is in the box labeled “Experiment” just below.
So what did you learn? Just type your answer in the code editor below and run it to submit and check your answer.
Do NOT put the [1]
as part of your answer, and note that R is case sensitive. Upper vs. lowercase letters matter.
If you still aren’t sure, then try again and type 5 / 0
in the Experiment box above or something similar to see the result.
Three answers are each correct depending on the situation. If you divide a positive number by zero, R returns Inf
(infinity). If you divide a negative number by zero, R returns -Inf
(negative infinity). If you divide zero by zero, R returns NaN
(not a number)!
As you learn to program, you’ll find it is helpful to try to think of possible edge cases that might give you different results than you expect. It is also be good to be aware of the possiblilities, because if you see NaN
come from your code, you’ll know you probably divided by zero somewhere without expecting it.
A mystery operator
Now, I want you to try to figure something else out. What does the %%
operator do? You code with it just like +
, -
, *
, or /
with two numbers.
For now limit your experiments to the left number being larger than the right and use only whole positive numbers, i.e., positive integers, on both sides.
For example:
12 %% 5
Experiment for a bit until you are pretty sure you have it right, and then, answer the questions below.
Ready to test your knowledge? Answer the following questions.
What is 13 %% 3
? (Of course, you could just type it in the “Experiment” box above to find out, but try to figure it out without doing that!)
Are you sure you tried enough examples in the Experiment box above? Try 12 %% 5
, 11 %% 5
, 10 %% 5
, and 9 %% 5
then try 12 %% 6
, 12 %% 5
, 12 %% 4
, 12 %% 3
, 12 %% 2
to see if you can find a pattern. It’s related to division.
The answer is 1
because 3
goes into 13
four times with a remainder of 1
.
1
1
OK, what is 8 %% 8
?
0
0
Last one: Even though I only asked you to use integers when you played with it works the same for fractional values… so what is 7.1 %% 0.2
?
0.1
0.1