Chapter 4.1 - R Programming for Absolute Beginners - R as a Calculator
In this series of posts, I'll share some chapters from my R Programming for Absolute Beginners Book
My R Programming for Absolute Beginners book is tailored for people that want to learn the R programming language and that have never had any contact with the language before.
From understanding R objects, to work with external libraries, I explore all the important aspects of R, completely from scratch. Every instruction will be thoroughly explained with coding examples, making sure that we compound our knowledge on solid fundamentals, helping you learn through practice. In this series of Posts in my Substack, I’ll share some of the chapters with you and, today, we’ll learn how to use R as a calculator, the simplest R use case we can think of.
If you enjoy these chapters and would like to buy the full book, you can find it on:
4.1. R as a Calculator
Let's begin with the most basic use case: we'll use R as a calculator. This introduces us to coding in R, demonstrating that it adheres to the universally understood mathematical principles, such as order of operations, composite functions, and the four fundamental arithmetic operations (addition, subtraction, multiplication, and division).
So, what's the simplest task we might undertake? How about adding two numbers in R? And while we're on the topic, let me introduce another feature: the 'code' icon, which always precedes a block of code.
20+30
Every time you see this block of code, feel free to open RStudio and type the code that sits in the gray box on the script console, like this:
Remember that you can run code either by using the Run icon, or selecting the code and hitting CTRL + Enter. What happens when we execute 20+30? We’ll have the expected output, 50.
Just like in normal mathematics, “+” is recognized as the Addition arithmetic operator and 20+30 will add the two numbers:
[1] 50
The number inside the [ ] indicates the line of the output. For example, [1] refers to the first line and if the output spans multiple lines, you'll see more numbers within [ ]. There are a few exceptions, which we'll delve into later in the book but rest assured, the output you see here mirrors exactly what appears in the R console (specifically, the bottom right window of RStudio).
What happens if we want to subtract two numbers? Well, as expected, we need the minus “ – “ symbol:
100-50
[1] 50
Keep in mind that in the R Script window, you can execute code by highlighting the desired section and pressing 'CTRL+Enter', or by clicking the 'RUN' button located at the top right of the script window.
Is R limited to performing mathematical operations with only integers? Absolutely not! R can also handle floating-point[1] numbers with decimal places, and it behaves as you'd expect. For example:
10-5.3
[1] 4.7
Notice that our output now contains 4.7. The decimal point is a good hint that we are dealing with a float number in the output. “Float” is a common way to say “numbers with decimal points” in the context of programming languages.
The operator for division, in R, is / - this operator allows us to divide two numbers, for instance:
10/2
[1] 5
We only have one basic arithmetic operator left - the multiplication! Multiplying two numbers in R is easy by using the * operator:
2*3
[1] 6
It's quite straightforward, isn't it? I encourage you to experiment with all the primary arithmetic operators. Play around by combining both floating-point[2] numbers and integers to see how the results vary.
In this chapter we’ve performed simple calculations using the R language but are we limited to just these basic operations? Absolutely not - let's dive into more complex calculations in the next chapter!
Read more on GumRoad or Amazon.com.
[1] Numbers with a decimal point
[2] Numbers with a decimal point