Chapter 3 The Console
Click on the console, type 1+1
, and hit enter
1 + 1
## [1] 2
It should have hopefully returned the answer 2. Next type 2*1
, and hit enter. This should return the sum value.
2*1
## [1] 2
Now, hit ctrl + uparrow (cmd + uparrow on mac). This is a useful shortcut, and allows you to quickly re-run or edit previous code used in the console. This handy little shortcut isn’t needed in the script, as you’ll be able to easily copy, change, or run code as needed.
3.1 Spacing
You can use spaces to make code easier to read, and although R
is pretty good at ignoring large gaps, it does have to guess and make the assumption that the spacing was unintended. Try to keep your code neat and tidy!
2 * 1
## [1] 2
What you can’t do is insert spaces into the middle of a word, or R
will get upset with you.
3.2 Typos
You’ll also need to be careful to avoid typos, as R
will not know that it is producing unintended output. Instead, it will assume that you meant exactly what you typed. For example, suppose you forget to press the shift key when typing the +
sign. Your command would then be 20 = 10
, as opposed to 20+10
. Here is what would happen:
20 = 10
## Error in 20 = 10: invalid (do_set) left-hand side to assignment
And there you have it - your first error message! This happens when what you type doesn’t make any sense to R
. It’s a pretty touchy programme, and can’t spot these kinds of simple human mistakes. Sometimes, R
will produce the right answer, but to the wrong question, if you are not careful with what you type. Sticking with the same example, suppose your hand slipped and you pressed the “-” key next to the +
. R
has no way of knowing that you intended to add 10 to 20, not to subtract. This time you’d get:
20 - 10
## [1] 10
This can be a little more dangerous, especially when you are working on more advanced stuff, as it can become more difficult to spot mistakes in output. The take home message is simple: You must be precise and accurate with what you say to R
. It’s mindlessly obedient, and doesn’t have auto correct. Be careful with what you type!
3.3 Unfinishe…. d
Sometimes, you might get a little too excited and hit enter when you haven’t actually finished a command. Because R
is so obedient, it will just keep waiting. For example, if you type 20 +
and then press enter, R
will know that you probably want to put another number at the end of that command.
and there in the console you should see a blinking cursor on the right of your +
sign. This tells you that R
is still waiting for you to finish your command, and the +
sign is another command prompt. If you now type 10
and press enter, you will get:
3.4 Basic Arithmetic
Table 1 lists the operators that correspond to the basic arithmetic we have used regularly for a number of years:
Operation | Operator | Example Input | Example Output |
---|---|---|---|
Addition | + | 20 + 10 | 30 |
Subtraction | - | 20 - 10 | 10 |
Multiplication | * | 20 * 10 | 20 |
Division | / | 20 / 10 | 2 |
Power | ^ | 20 ^ 10 | 1.024e+13 |
The one important thing to remember when using R
to calculate sums is that brackets always come first. One easy way to remember this is to enclose what you want to happen first in brackets e.g. (20/10) * 2
. In this example, R
would have done the division first anyway, but its always important to make sure that R
is doing exactly what you want!
3.5 Using Functions for Calculations
As you will have seen above, there are lots of calculations you can do with very basic operators. If you wanted to do more advanced calculations, you need to use functions. Lets run through a few of the simple ones that are handy to use in R
.
Operation | R code | Example Input | Example Output |
---|---|---|---|
Square root | sqrt( ) |
sqrt(100) | 10 |
Absolute value | abs( ) |
abs(-100) | 100 |
Round | round(x, digits = ) |
round(12.345, 2) | 12.35 |
Min | min(...) |
min(2.21, 2.22) | 2.21 |
Max | max(...) |
max(2.21, 2.22) | 2.22 |
It’s also useful to note that you can use multiple functions together and combine them if desired. For example:
sqrt(100 + abs(-44))
## [1] 12
R
has started out by calculating abs(44)
, and then simplifies the command to sqrt(100+44)
. To solve the sum, it then needs to add 100 and 44, before evaluating the sqrt(144)
, and returning the value of 12.
Top Tip: Lets take an example from above, and say that we wanted to round a value. This time, in the console, start typing the name of the function, and then hit the ‘tab’ key. RStudio will display the below window.
There are two panels: one gives a list of variables that starts with the letters I’ve typed, and the other tells you what it does. If you have the name of the function written, R
will recognise this, and instead pop up with the arguments for that function instead.