Basic skills in R: Your first calculations with R
Using R as a calculator
Recap Where do you enter your R code and how to run it (Chapter: Getting familiar with the working environment)?
- Where: Enter your code in the source editor (usually the upper-left panel) and preferably not in the Console panel (usually the lower-left panel).
- How to run: Place the cursor on the line that you want to run and Press Ctrl+Enter (Windows) or Command+Enter (MacOS).
How to do calculations? You can learn this in two ways: If you prefer to
- watch a video and repeat the steps for yourself, then watch the video clip below;
- (scan) read things while repeating the steps yourself, then you can continue reading this page.
Arithmetic calculations
Explanation
R can perform simple arithmetic operations, just like a calculator. On the right you see some examples of how to do addition, subtraction, multiplication, division and exponentiation in R. Enter the calculations in the source editor and check your answers.
Examples
# Addition
5 + 3
# Output: 8
# Subtraction
10 - 4
# Output: 6
# Multiplication
2 * 6
# Output: 12
# Division
15 / 3
# Output: 5
# Exponentiation
2^3
# Output: 8
mathematical functions
Explanation R can do way more than simple arithmetic calculations. In R there are several built-in functions that can help you to do mathematical calculations. We will explain more precisely what a function is later on in this chapter, but for now you can think of a function as a (magical) machine that takes some input, does something special with it, and then gives you an output. For example, consider the function |
Examples # Square Root |