This book is in Open Review. We want your feedback to make the book better for you and other students. You may annotate some text by selecting it with the cursor and then click "Annotate" in the pop-up menu. You can also see the annotations of others: click the arrow in the upper right hand corner of the page

1.2 A Very Short Introduction to R and RStudio

RStudio: the four panes

Figure 1.1: RStudio: the four panes

R Basics

As mentioned before, this book is not intended to be an introduction to R but a guide on how to use its capabilities for applications commonly encountered in undergraduate econometrics. Those having basic knowledge in R programming may feel comfortable starting with Chapter 2. This section, however, is meant for those who have not worked with R or RStudio before. If you at least know how to create objects and call functions, you can skip it. If you would like to refresh your skills or get a feeling for how to work with RStudio, keep reading.

First of all, start RStudio and open a new R script by selecting File, New File, R Script. In the editor pane, type

1 + 1

and click on the button labeled Run in the top right corner of the editor. By doing so, your line of code is sent to the console and the result of this operation should be displayed right underneath it. As you can see, R works just like a calculator. You can do all arithmetic calculations by using the corresponding operator (+, -, *, / or ^). If you are not sure what the last operator does, try it out and check the results.

Vectors

In R, you can work with variables or more generally, objects. To define an object, you use the assignment operator <-, for example, to create a variable named x with the value 10, you can type x <- 10 and then click the Run button. The new variable should appear in the environment pane on the top right. However, the console won’t display any results because this line of code doesn’t produce any visible output. If you want to see the value of x, you can simply type x in the console and press Enter, and R will display the corresponding value in the console.

x is a scalar, a vector of length \(1\). You can easily create longer vectors by using the function c() (c is for “concatenate” or “combine”). To create a vector y containing the numbers \(1\) to \(5\) and print it, do the following.

y <- c(1, 2, 3, 4, 5)
y
#> [1] 1 2 3 4 5

You can also create a vector of letters or words. For now just remember that characters have to be surrounded by quotes, else they will be parsed as object names.

hello <- c("Hello", "World")

Here we have created a vector of length 2 containing the words Hello and World.

Do not forget to save your script! To do so, select File and then Save.

Functions

You have seen that the function c() can be used to combine objects. In general, all function calls look the same: a function name is always followed by round parentheses. Often the parentheses include arguments.

Here are two simple examples.

# generate the vector `z`
z <- seq(from = 1, to = 5, by = 1)

# compute the mean of the entries in `z`
mean(z)
#> [1] 3

In the first line we use a function called seq() to create the exact same vector as we did in the previous section, calling it z. The function takes on the arguments from, to and by which should be self-explanatory. The function mean() computes the arithmetic mean of its argument x. Since we pass the vector z as the argument x, the result is 3!

If you are not sure which arguments a function expects, you may consult the function’s documentation. Let’s say we are not sure how the arguments required for seq() work. We then type ?seq in the console. By hitting return, the documentation page for that function pops up in the lower right pane of RStudio. In there, the section Arguments holds the information we seek. On the bottom of almost every help page you find examples on how to use the corresponding functions. This is very helpful for beginners and we recommend to have a look.

Of course, all of the commands presented above also work in interactive widgets throughout the book. You may try them below.