1  Getting started with R

1.1 Installing and Setting Up R & RStudio

R is a completely free program that you can download from CRAN. The RStudio extension is also free and can be downloaded here. RStudio enhances R by providing a significantly more informative and appealing interface, help, and auto-completion when writing syntax, as well as an overall improved user interface. However, RStudio is an extension of R, so you need both programs.

Install R first and then RStudio, so that RStudio recognizes the installed R version, and the two programs usually connect automatically. R is essentially the engine, and RStudio is our cockpit. We could work directly with R, but RStudio offers a more comfortable option and a better overview.

Figure 1.1: R and RStudio

1.2 Setting Up RStudio

After successful installation, open the RStudio application and you should see the following view:

To avoid problems when working with R in the future, please disable the automatic saving and loading of the workspace. To do this, go to the appropriate menu under the “Tools -> Global options” tab, disable “Restore .RData into workspace at startup,” and set “Save workspace to .RData on exit:” to Never. Otherwise, RStudio will save all loaded objects when you end the session and automatically load them the next time you open the program, which can lead to problems.

Confirm the settings with “Apply” and close the window with “OK.”

1.3 First Steps in R

After these basic settings, we can start with the first steps in R. To do this, first open a script by clicking on the white icon in the top left corner or pressing CTRL/Command + Shift + N simultaneously.

A fourth window opens, so you should now see the following view:

This script editor is where we will create and execute commands. The script editor serves as a collection of all commands to be executed. We can save these collections to revisit them later, and, more importantly, we can share command collections with others or use scripts from others for ourselves. So, we first draft a calculation in the script editor:

To execute it, click on the line to be executed so that the cursor is in that line, and then press CTRL and Enter simultaneously (Mac users: Command and Enter):

Figure 1.2: Shortcuts for Calculations

R outputs the results in the console below:

This also works for multiple calculations at once by selecting multiple lines and then pressing CTRL and Enter again (Mac users: Command and Enter):

Inputs from the script editor and results from the console will be presented like this in the future:

2+5
[1] 7
3-4
[1] -1
5*6
[1] 30
7/8
[1] 0.875

Of course, R also handles longer calculations, such as following the order of operations:

2+3*2
[1] 8
(2+3)*2
[1] 10

Other operations are also possible:

4^2 ## 4²
sqrt(4) ## Square root 
exp(1) ## Exponential function (Euler's number)
log(5) ## Natural logarithm
log(exp(5)) ## log and exp cancel each other out

We can create sequences of numbers using seq() or ::

2:6
[1] 2 3 4 5 6
seq(2,11,3)
[1]  2  5  8 11

1.3.1 Creating Objects

So far, we have always displayed our calculations directly. For more extensive calculations—since we want to work with datasets starting in the next chapter—we want to save the intermediate steps.

Results can be saved as objects under any name using <-. R will then not display the result but will repeat the command in the console:

x <- 4/2

In the “Environment” window at the top right, you can now see the stored object x:

We can retrieve it later:

x
[1] 2

Additionally, we can use objects in calculations—we simply use x and create, for example, y:

y <- x * 5
y
[1] 10

1.3.2 Storing Multiple Values

With c(), we can store multiple values under one object, and these can also be used in calculations:

x1 <- c(1,2,3)
x1
[1] 1 2 3
x1* 2
[1] 2 4 6

With length(), we can check the number of stored values:

length(x1)
[1] 3
y1 <- c(10,11,9)
y1
[1] 10 11  9
y1/x1
[1] 10.0  5.5  3.0

ls() lists all existing objects, we can use the pattern = option to display only objects with a name that contains “1”:

ls()
[1] "path" "x"    "x1"   "y"    "y1"  
ls(pattern = "1")
[1] "x1" "y1"

1.3.3 Deleting Values

Of course, we can also delete objects using rm(). If we try to call a non-existent object, we will get an error message:

rm(x1)
x1
Error in eval(expr, envir, enclos): Objekt 'x1' nicht gefunden

With rm(list = ls()), all objects can be removed from the environment.

1.3.4 Saving Scripts

We can save the script to call it again later.

It is important to give the saved file the extension “.R”, for example, “01_Script.R”.

1.3.5 Comments

Besides the actual commands, comments are a central part of a data analysis syntax. This allows future users (especially ourselves in 3 weeks or 2 years) to understand what is happening. Comments in R can be added with #:

2+ 5 # this is a comment
[1] 7
2+ # a comment can also be placed here
  5
[1] 7
( 2 + # a 
    3) * # comment
  2 # across multiple lines
[1] 10

Tip: It’s best to create a folder right away where you can store all R scripts and datasets from this course.

1.3.6 Structuring Scripts

# Heading 1 ----

## Section 1.1 ----
3+2*4
3+2*3
## Section 1.2 ----
3+2*sqrt(3)

# Heading 2 ----
x <- c(2,6,8,2,35)
y <- seq(2,10,2)

y/x

1.4 Exercises

  • Store the number of students at the University of Oldenburg (15643) in stud.
  • Store the number of professorships at the University of Oldenburg (210) in prof.
  • Calculate the number of students per professorship at the University of Oldenburg using the objects stud and prof.
  • Store the result in studprof and recall the object again!
  • Do you see the created variables in the Environment window?
  • Store the student numbers of the University of Bremen (19173), University of Vechta (5333), and University of Oldenburg (15643) together in studs.
  • Store the number of professors at the University of Bremen (322), University of Vechta (67), and University of Oldenburg (210) together in profs.
  • Calculate the number of students per professorship for all three universities.
  • You also want to include the student numbers (14000) and professorships (217) of the University of Osnabrück in studs and profs. How would you do that?
  • Calculate the ratio of students to professorships for all four universities!
  • Delete the object stud. How can you tell that it worked?
  • Delete all objects from the Environment. How can you tell that it worked?