2.3 Using R

The base functionality of R is provided in this section. Additional functions and explanations relating to specific methods or algorithms are provided in their respectful chapters in this book.

2.3.1 R setup

Download and install R.

Download and install RStudio.

Open R and install tidyverse:

To install Rcpp run the following command in R:

Rcpp and Rtools are frequently needed to speed up some parts of the code, as well as compiling newer packages on older installations of R.

2.3.2 Using CRAN TASK Views to Install Various R Packages

Having many packages preinstalled is usefull if your current version of R gets outdated and newer packages would require recompilation. Furthermore, installing all of these packages will ensure that you will almost always have the required package at hand.

One very important downside - installing many packages may result in an extremely slow help functionality for R function documentation. Again, this may depend on your computer, whether you have a HDD or SSD, as well as your version of R and Python, so it is recommended to only install packages required for your data analysis. You can always install additional packages later on.

There is an incredibly large array of available packages in R. Fortunately, most of the most popular, or very specialized, packages are clustered into different categories in CRAN TASK Views. You can very easily install the ctv package:

Then, in order to install the packages most requently required when working with certain Topics. For example, most frequently encountered packages throughout this book are in:

Other usefull packages for Econometrics and Data Science are in the following Topics:

Furthermore, you can always install all of the packages if you want, though you will most likely not be able to use the help functionality inside RStudio.

Finally, to update packages to the newest version, you can use:

2.3.3 R language tutorial using swirl

Open RStudio and install the swirl package

A number of tutorials/courses are available here. To get a basic understanding of the R programming language, install the following courses:

To run a course type:

and follow the on-screen prompts and select the “R Programming” course. Once you selected the course, go through all of the different topics from 1 to 15. Topic 13: Simulation assumes that you are familiar with a few common distributions. Data generation and random variable simulation will be covered in later chapters, so focus on the simulation part of this topic.

Finally, when you complete a topic, you will be asked: Would you like to receive credit for completing this course on Coursera.org?. Select 1: No, since your completion of these topics is not a requirement (unless you need them for some courses on Coursera) but only as an optional general introduction to get more comfortable with the R language in these notes.

Remember that you can press Enter when you see ... in the console to continue and you can enter bye() to exit or info() to see all of the available commands.

2.3.4 Introductory RMarkdown tutorial

Open up RStudio and create a new .Rmd file:

Enter a title for your document:

This will create a template document.

The beginning of an .Rmd file may have the following code:

This means that we want to generate an .html document with a table of contents which floats when we scroll down or up.

If we want to generate a .pdf output we can use:

A more complete introduction to RMarkdown can be found here.

To run R code in a .Rmd (RMarkdown) file, you need to specify the code as:

```{r, eval = TRUE}
print("Hello, World!")

which gives the output:

## [1] "Hello, World!"

You can verify that it works with the template file that you created:

To compile the whole file, select the Knit option at the top. You can also click the small arrow to select the type of file that you want to create: