Lesson 1 - Using R as a Calculator
What better way to start learning R than to use it as a calculator?
Table of Contents
Lesson Objectives
- Learn about the mathematical operations R supports.
Math Operations
Math expressions in R work similarly to how math expressions work with other programs and languages.
Input
3 + 3
Output
[1] 6
R supports addition (+), subtraction (-), multiplication (*), division (/), exponents (^), modulus (%%), and integer division (%/%).
When there are multiple operations in an expression, R follows the typical order of operations listed below.
Operation | Symbol |
---|---|
Parentheses | ( ) |
Exponents | ^ |
Modulus and Floor Division | %%, %/% |
Multiplication, Division | *, / |
Addition and Subtraction | +, - |
Input
24 %% (4 * (1 + 5))
Output
[1] 0
R also supports decimal numbers as well as complex numbers. Complex numbers are briefly mentioned in Lesson 3a.
Input
12.3 ** 1.5
Output
[1] 43.13777
Other Math Functions
R comes with some built-in math functions, requiring no extra setup. Getting the absolute value of a number, finding the square root, rounding numbers, and more.
Input
sqrt(16)
Output
[1] 4
A full list of built-in math functions can be found here: https://www.javatpoint.com/r-built-in-functions.
Key Points / Summary
- You can use R as a calculator.