Learn R
Chris M. Stolle
2023-04-10
Intro
This tutorial gives a brief introduction into the statistical programming language R. It is aimed for people without any prior programming experience. Basic knowledge of applied statistics can be advantageous but is also not necessary.
This tutorial has the following structure: You are currently reading the intro. It should give you a brief overview and a motivation to conduct this tutorial. Second, we provide you the necessary links to start programming with R. You don’t have a computer available on which you can run R? - No reason to stop here. We also show you a possibility to run R in your browser. In the third section, we finally start to get our hands a bit dirty. There, we will learn to use R for basic calculations and store our first variables.
What is R?
R is one of the most commonly used programming languages. After downloading R, users receive a so-called base package that allows to conduct a wide range of tasks. Further, the community can develop new packages. Currently, there are more 19’300 packages available on the official R-servers. In addition to those, there are numerous unofficial packages available on fora like github.
Who should learn R?
R is of interest for everyone who wants to do statistics-related programming. There is a wide range of videos and articles available that compare programming languages, their pros and cons and recommend what language to learn. People read these article or watch these videos, and after a long time of research they learn … no programming language but keep procrastinating. Therefore, we will keep it short:
Do you want to conduct statistical analyses, write statistical simulations and do some visualisations? – Learn R! Simply continue with this tutorial.
Do you want to do deep learning? – Learn python! A good start could be the programming tutorials from freeCodeCamp.
Are you not sure which to choose? – Learn the language that is currently most useful for you. Switching languages later on is not a problem and much easier than learning a language from scratch. By the way, the author of this site uses both languages actively 🙂
Installation
We generally recommend you to download R and use it locally. This way, you do not only have full control over your hardware and code, but you can also easily save and load your current workspace which allows you to continue your work from where you have stopped.
Local installation
You may start with downloading R. You can find it under this link. If you are using Linux, you can easily install R through your Software-Center or by typing in the corresponding command in your terminal (e.g. for Ubuntu: sudo apt-get install r-base).
After downloading R, please follow the normal installation instructions. When the installation is finished, you can already use R. Windows and Mac users find their R-installation in their installed software. You may open the R-program, which will show you a console. A fresh new line in the console start with the > sign.
Linux users can open a terminal and type in R. This will open the R-console within the terminal.
To check if everything works right, you can type in the command (see grey square) and press enter. As a response, you should get the response as indicated in the white square.
print('Hello World')
## [1] "Hello World"
When doing some real tasks, it is easier to use an integrated development environment (IDE). A widely used IDE for R is RStudio. We recommend to download it under this link. After downloading, you may follow the installation instructions as usual. Linux users can again download RStudio directly from their software center / package manager.
When you open RStudio, it may ask you to create a project. You can either choose an empty directory that you have already created, or let Rstudio create a new subdirectory. Choose a directory where you want to have your project saved. RStudio will automatically choose this directory as its working directory and automatically save files, such as an Rproj file or a Rhistory file, in there. If you cannot see the file endings, you have to change the corresponding settings to view file extensions. You can now close RStudio. By clicking on the Rproj file, RStudio will open again and you can continue you work where you left.
RStudio is divided into four sections. The upper left is the editor. Here you can write you code. Below, you find the console which you already now from the previous steps. You can enter commands directly into the console. Furthermore, you can also mark code in the editor, hold [Ctrl] and [Enter]. This will execute your code in the console. If you only hold [Ctrl] and [Enter], the current line is executed in the console. You can try it out by typing the previous hello world command and pressing [Ctrl] and [Enter].
On the right, you can see the R environment. This will become relevant later. Below, you can see the files that are saved in your work directory.
Using R online
Google colab gives the option to use R online. You can simply open it under this link:https://colab.research.google.com/#create=true&language=r
It is necessary to log in with your google account. After logging in, the page might take a few seconds to open. Once opened, you will see an empty cell with a play button. Please enter the hello world example from above and click on the play button (or press [Ctrl] and [Enter]). The first execution will take some time, but it will give you the same response as above. When you are executing new code while still being in the same session, things will go a lot faster.
In the upper left, a title of your project appears (e.g. Untitled0.ipynb). You can click on this title and change it.
The code you write will be saved. You will find your previously opened projects with their project title under this link:https://colab.research.google.com/
What will not be saved are any files you may create and not download, and the current status of your code. However, an advantage of using google colab is the clean interface.
In case strange behaviour appears in google colab, its most likely that you have chosen a wrong runtime type by accident. Click on Runtime - Runtime settings and check that your Runtime type is R!
I hope you can now successfully execute the hello world line we have provided above. Next, we will use R and do some simple calculations.
Basic Calculations
The most simple tasks that can be done in R are basic calculations. You only have to enter the arithmetic calculation into the console (or into the editor and execute it from there) and R will immediately give you the response.
The four basic arithmetical operations
Let us have a look at some simple examples from the four basic arithmetical operations:
3+3
## [1] 6
5-2
## [1] 3
5*3
## [1] 15
15/5
## [1] 3
Many programming languages make a differentiation between integers and floating-point numbers. R does not make a real differentiation between these type, so users can mix floats and integers:
3+3.1
## [1] 6.1
5-2.1
## [1] 2.9
5*3.3
## [1] 16.5
15/5.2
## [1] 2.884615
Power and root
Getting a number to the power of another number is fairly simple. Let’s say you would like to get the result of 53, you can enter either of one commands:
5**3
## [1] 125
5^3
## [1] 125
5*5*5
## [1] 125
If you would like to receive the square root, for example the square root of 25 (√25), you can simply enter:
sqrt(25)
## [1] 5
Sometimes, you might need the result of higher order roots. In that case, you can simply enter the number to the power of 1 divided by your root order. Let’s say you want to get the results for ∛125, you can do following:
125**(1/3)
## [1] 5
It is very important to add round brackets around 1/3. We will provide a deeper explanation for this in the subsection operator precedence.
Special numbers
Two of the most often used special number are e and π. In R, they can be use like normal numbers by using the following commands:
For π, you may enter…
pi
## [1] 3.141593
Euler’s number is always entered to the power of another number. Let us look how we can get e:
exp(1)
## [1] 2.718282
If you want to get e2, you can simply enter the following command:
exp(2)
## [1] 7.389056
Logical Operations
Comparisons
We can do simple comparisons (is x smaller/larger/equal to y) as shown in the following. What it will return is either True or False.
If you want to check for equality:
5==5
## [1] TRUE
5==3
## [1] FALSE
If you want to do the opposite and check for inequality:
5!=5
## [1] FALSE
5!=3
## [1] TRUE
If you want to check if a value is bigger than another value:
5 > 3
## [1] TRUE
5 > 6
## [1] FALSE
5 > 5
## [1] FALSE
If you want to check if a value is smaller than another value:
5 < 3
## [1] FALSE
5 < 6
## [1] TRUE
5 < 5
## [1] FALSE
You have seen that the comparison if a value is bigger or smaller than another value is rather strict. This means, values that are equal will neither show up to be bigger nor smaller. However, you can also check if values are bigger or equal, or smaller or equal:
5 >= 3
## [1] TRUE
5 >= 6
## [1] FALSE
5 >= 5
## [1] TRUE
5 <= 3
## [1] FALSE
5 <= 6
## [1] TRUE
5 <= 5
## [1] TRUE
Operator precedence
Every programming language has