The first command is simple addition, so the exercise is to add some numbers together. Open up R, and use it to do a few basic sums. In particular, it’s worth trying the following:

- Try adding more than two numbers.
- Try playing around with the number of spaces between numbers and the plus sign, and verify that it doesn’t actually matter.
- Try hitting enter when you’re half way through the command (e.g.,
`10+`

). R will wait for you to finish before trying to execute it.

For this exercise, the solution set displays the R prompt (i.e., the `>`

) and displays the results more or less exactly the way that the would appear in R. For the later exercises, I’ll tidy the output a bit more to make it a little more human-friendly. The basic convention you’ll see throughout the output is that the grey boxes contain R commands, and the white boxes contain R output. (This is a pretty standard format for “R Markdown” files, which I talk about briefly at the end of the workshop).

First, here’s the basic addition from the slides:

`> 10+3`

`## [1] 13`

(Note that when you see the output on screen you don’t see the `##`

part).

Here’s an example of adding more than two numbers:

`> 10+3+4`

`## [1] 17`

Here’ an example showing that spacing doesn’t really matter all that much if you do it sensibly:

`> 10 +3`

`## [1] 13`

Finally, here’s an example where I’ve hit enter before finishing the command. Sometimes (but not always), R is “smart” enough to figure out that the command isn’t complete, so it waits for the user to finish before continuing:

```
> 10+
+ 3
```

`## [1] 13`

- Multiply 3 by 14 and subtract 5 from the result
- Raise 5 to the power of 4
- Divide 267563 by 1235

Here’s the answer to the first part:

`3*14 - 5`

`## [1] 37`

Notice that - as I mentioned above - the solution set now drops the `>`

from the output. I’ll do it this way from now on.

Next, here’s 5 raised to the power of 4

`5^4`

`## [1] 625`

And finally, here’s the silly division problem:

`267563 / 1235`

`## [1] 216.6502`

- Use logical operations to get R to agree that “two plus two equals five” is
`FALSE`

- Use logical operations to test whether 8 raised to the power 13 is less than 15 ^ 9

`2+2 == 5`

`## [1] FALSE`

`8^13 < 15^9`

`## [1] FALSE`

- use the
`sqrt()`

function to calculate the square root of 789 - round 2.456 to the nearest whole number using
`round()`

- round 2.456 to two decimal places using
`round()`

- R has a
`factorial()`

function that calculates the factorial function,`n!`

(e.g.,`5! = 5*4*3*2*1`

). Use it to calculate`25!`

, and take note of the way that R formats the answer. - Use
`factorial()`

to (try to) calculate`2000!`

, and note the answer that R gives.

Here’s the square root calculation:

`sqrt(789)`

`## [1] 28.08914`

Here are the solutions to the problems involving rounding:

`round(2.456)`

`## [1] 2`

`round(x = 2.456, digits = 2)`

`## [1] 2.46`

Here’s what happens with the factorial calculations:

`factorial(25)`

`## [1] 1.551121e+25`

`factorial(2000)`

`## Warning in factorial(2000): value out of range in 'gammafn'`

`## [1] Inf`

The warning message there is worth commenting on. Notice that it says that the warning comes from a function called `gammafn`

, not from `factorial`

? What that means is that the `factorial`

function is using `gammafn`

to do some of the calculations, and it is *that* function that has issued the complaint. That’s very typical of R warnings and error messages. The message that gets printed to the screen comes from the function that issued the complaint, which might not be the one that *you* called.