Chapter 1 Setting up R

This tutorial will help you set up your computer to use R. It is for you if you need to:

  • Install R on your computer
  • Install the RStudio IDE
  • Install the tidyverse R package

You can skip this tutorial if you’ve already done these things.

1.1 R

Learn how to set up R and RStudio on your machine. I will also demonstrate how to install R packages from CRAN, and install the tidyverse package.

At its heart, R is a calculator and can accept any mathematical calculation directly into the console. The following table represent just a few of the available mathematical operators:

Commands can be run directly from the console, but creating an R Script allows you to edit and reuse previous commands and to create more complicated lists of commands. A script also allows you to save your commands to be reopened later.

Multiple commands can be entered into a script, one after the other across multiple lines.

The script above creates a new variable called variable1 with the value “This is a character variable”. The next command counts the number of characters in the variable. The final command returns the value of variable1.

To run the script one line at a time, navigate the cursor to the appropriate line and press CTRL + Enter. To run all commands from the start, press CTRL + Shift + Enter.


  1. Create a new script
  2. Enter the following code into your script: variable2 <- c(12,1,15,3,20,6,11) variable2 mean(variable2) variable2 + 2
  3. Run your code (Ctrl + Shift + Enter)
  4. Save your script

1.2 RStudio

1.2.1 The RStudio IDE

You can use only R, but using RStudio can save you a lot of headache and a lot of time! Best analogy ever:

RStudio is an Integrated Development Environment for R. What does that mean? Well, if you think of R as a language, which it is, you can think of RStudio as a program that helps you write and work in the language. RStudio makes programming in R much easier and I suggest that you use it!

When opening R Studio, you will be presented with the following screen. The colours and fonts may vary depending on your Operating System but the layout will be the same.

On creating a new R script (see next section), the script panel will open. You will write your R code/script in here and it will be run in the console.

1.2.2 Install packages

The basic capabilities of R are quite limited. They are extended by packages, of which there are currently over 12,000. You can browse them by category in CRAN Task Views or in the convenient METACRAN and search engine.

1.2.3 RStudio Projects

R experts keep all the files associated with a project together — input data, R scripts, analytical results, figures. This is such a wise and common practice that RStudio has built-in support for this via projects.

Let’s make a project for you to use while you’re working through the rest of this book. Click File > New Project, then:

Call your project and think carefully about which subdirectory you put the project in. If you don’t store it somewhere sensible, it will be hard to find it in the future! Once this process is complete, you’ll get a new RStudio project!

Projects are a powerful tool in RStudio for developing reproducible code, whether for individual data analyses, data-driven reports, or even developing your own packages. Projects also get you ready from the beginning of a collaborate with you, for instance, on GitHub. But the main advantage of projects is that they make your life easier when importing data.

1.3 Git & GitHub

Ok, you already know the basics of programming in R, the R-Studio environment. You create joint projects with your friends, but you keep getting confused between versions. You would like to work on it together, see and note your progress in real time. The answer is Git. Git is a distributed version control system and invaluable support for any developer! It’s what allows you to create a project build history, manage versions, control the progress and edit the code you create, and then post it to GitHub.

What are Git and GitHub? First of all, they are two separate things:

  • Git is open source software for version control. Using Git, you can do things like see all previous versions of code you’ve ever created in a project.
  • GitHub is the most popular service (others include GitLab and BitBucket) for collaborating on code using Git.

It’s possible to use Git without using GitHub, though most people combine the two. Being able to have a record of all of the changes you’ve ever made to your code both locally and on a remote website is powerful.

Please go to the textbook “Happy Git with R” to read more about how to use Git and GitHub with R and R-studio:

1.4 Resources

  1. First, don’t forget to install R & R-Studio.

  2. If you are new to R - please read the complete tutorial on R here. If you are more familiar with Python, you can use it in the lab as well. I really recommend to use this e-book: “Introduction to Probability and Statistics Using R” or textbook: “Practical Statistics for Data Scientists 50+ Essential Concepts Using R and Python”.

  3. Recommended DataCamp courses: “Exploratory Data Analysis in R”; “Intermediate Data Visualization with ggplot2”; “Introduction to Statistical Modeling in R”; “Introduction to Regression in R”; “Correlation and Regression in R”; “Time Series Analysis in R” [ask me for free access or contact our Data Science Club]

  4. The R-Bootcamp is a gentle and gradual introduction to manipulating and visualizing data in R.

  5. A collection of over 250 (mostly) free R programming books - “Big Book of R”.

  6. If you are looking for help, use search engines like R-Seek, Search-R & forum Stack Overflowor R-Bloggers.

  7. I also recommend “cheat-sheets” - posters that will guide you through some R external packages - the whole collection of them is available here.

  8. For those who are more interested in data analysis, I recommend joining our “Data Science Club” - details on our fanpage, FB group, website or Discord channel.