Chapter 4 Using R as a calculator

The first thing to know about the R console is that you can use it as a calculator.


4.1 Arithmetic Operators

  • Additions and Subtractions
1 + 1
## [1] 2
10 - 1
## [1] 9
  • Multiplications and Divisions
2 * 2
## [1] 4
8/2
## [1] 4
  • Exponents
2^3
## [1] 8

4.1.1 CHALLENGE 2

Use R to calculate the following skill testing question:

\(2 + 16 * 24 -56\)

Solution

2 + 16 * 24 - 56
## [1] 330

4.1.2 CHALLENGE 3

Use R to calculate the following skill testing equation:

\(2 + 16 * 24 - 56 / (2 + 1) - 457\)

Pay attention to the order of operations when thinking about this question!

Solution

2 + 16 * 24 - 56/(2 + 1) - 457
## [1] -89.66667

Note that R always follows the order of priorities.

R TIP

Try using the “up” and “down” arrows to reproduce previous commands. These keys actually allow you to scroll through your command history. This is a useful tool to go back and see what command you ran and if you might have made a mistake in it. This is always a useful tool to quickly alter previous commands you ran and to re-run them in a slightly different way.


4.1.3 CHALLENGE 4

What is the area of a circle with a radius of 5 cm?

Recall : \({Area _{circle} = \pi r^2}\)

Solution

3.1416 * 5^2
## [1] 78.54

R also has many built-in constants like pi.

pi * 5^2
## [1] 78.53982

You can find them by typing ? and Constants (as in ?Constants) and executing it! `