Programming basics in R - The Basics

Tiago Maié

Fabio Ticconi

Johannes Schöneich

18 October 2022

Intro - Before you start

This tutorial contains different levels of difficulty that build on each other. If you skip a level it might be possible that you won’t manage to finish the next one.
Further, Level 1 should be done BEFORE the class takes place. The class will start on level 2. Don’t ruin your chances of getting further in the class and learning more about this subject that you chose to learn by skipping level 1.

Please do Level 1 before the class takes place.

Must know:

  • Programming is simply giving the computer a basic “cooking” recipe to execute.
  • As with a cooking recipe, it has to be done sequentially. If you were making a simple bread, you wouldn’t bake the water, then add the flour and then call it a bread.
  • As with a cooking recipe, if you write each instruction in a readable way, it will help you read the “recipe” later on. Tablespoon and tbsp usually mean the same but if you’ve never cooked before or haven’t cooked for a while, the first is more understandable than the latter.
  • The computer is not smart and (usually) cannot guess what you are trying to say. To keep the cooking analogies going, if you make a mistake by adding grapes to a pizza (don’t do this, please!) in the end you will either have the wrong result or the computer will stop you from doing it (unfortunately no computer was present when they created spaghetti pizza).
  • If you follow the exact same recipe as the person next to you, you both will end up with the same exact result (unless you are working with random numbers).
  • As with cooking, with train, practice and imagination, you can speed up and spice up your recipes. As with cooking, don’t be afraid to get your hands dirty.
  • Cooking (and other) analogies will be used during this tutorial :) brace yourself!

For students being evaluated:

  • Every Exercise section is mandatory and should be submitted for evaluation.
  • The submission should be a single script per class/day (e.g. day1_exercises_[last name]-[first name].R).
  • In this script only the code with your solution for each of the Exercise sections should be present as well as comments to clearly state to which exercise each solution corresponds to.

1 - Level

At this point you are supposed to have installed R and RStudio on your computer. If you haven’t done that yet, please go and do it before you move forward. You can install R from and RStudio from (the links here will take you to the correct installation page).

1.1 R and RStudio

R is a (statistical) programming language. RStudio is the environment where you will be typing your code.
Imagine RStudio is like Word. Makes your text pretty but if you don’t put anything inside it doesn’t do much.
R is great if you want to do data analysis and produce state-of-the-art plots.