# Chapter 1 R Environment

## 1.1 Installation

For this course we need to install two things: R and Rstudio.

## 1.2 Rstudio

RStudio is an Integrated Development Environments (IDEs) built for R. You can think of Rstudio as our gateway to R; we are going to ask R to do computations through Rstudio.

Basic Rstudio interface:

• The lower left pane is the R console, which can be used just like the standard R console.

• The upper left pane takes the place of a text editor.

• The upper right pane holds information about the workspace, command history, files in the current folder and Git version control.

• The lower right pane displays plots, package information and help files.

For now the cosole and the editor will be the main components that we are going to work with.

### 1.2.1 R Console

Things that we type into the R prompt are called expressions. Let us start with typing the following expression

x <- 2

The symbol <- is called the assignment operator. The assignment operator is what assigns a value to a symbol. For example, in the expression above the symbol that we created is called x, and the value that we assigned it is 2.

Now this sybold is in the current memory of R and if you want to display it simply type it and hit enter key to print out the value assigned

x
[1] 2

We could also use print function to display

print(x)
[1] 2

In this case, we are explicitly calling the print function to operate on x.

Notice that when we print out x, there is a little one in brackets there. It tells which element of the vector is being shown. In this simple example, it is saying that the number 2 that we are seeing there is the first element of the vector. This will make more sense when we have longer vectors to look at. Let us create a vector of numbers from 1 to 40.1

# we use the command "n:m" to create a sequence of integers from n to m
x <- 1:40
x
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

The second line has a 24 in brackets because that is the first element of that line is the 24th element of this vector.

To repeat a line of code, simply press the Up Arrow key and hit Enter again. All previous commands are saved and can be accessed by repeatedly using the Up and Down Arrow keys to cycle through them.

Interrupting a command is done with Esc in Windows and Mac and Ctrl+C in Linux.

### 1.2.2 The Editor

The editor is very useful when we have a long code to execute or when we are trying to figure out a problem or when we want to take notes inserted as comments between R codes.

There are a number of ways to send and execute commands from the editor to the console. To send one line, place the cursor at the desired line and press Ctrl+Enter (Command+Enter on Mac). To insert a selection, simply highlight the selection and press Ctrl+Enter. To run an entire file of code, press Ctrl+Shift+S.

We can add comments to our R code by using the hash symbol, which indicates that everything to the right of that is a comment and should be ignored. Comments are especially useful when you write long codes or when you want to communicate your code with other people. For example, below I will write some R code together with their explanations:

# this is my first R script
# we can assign words too
x <- "hello"
# display x by calling the print function.
print(x)
[1] "hello"
# now assign another value to x
x <- 3
# R will override the previous value
x
[1] 3

To save the script file: File -> Save and hit Save button at the lower left corner. But note that it is going to save to the current working directory, so if you want to save the script somewhere else you need to specify that first.

## 1.3 Keyboard Shortcuts

Here is a list of useful keyboard shortcuts:

Console

Description Windows & Linux Mac
Move cursor to the text editor Ctrl+1 Ctrl+1
Move cursor to Console Ctrl+2 Ctrl+2
Clear console Ctrl+L Ctrl+L
Move cursor to beginning of line Home Command+Left
Move cursor to end of line End Command+Right
Navigate command history Up/Down Up/Down
Popup command history Ctrl+Up Command+Up
Interrupt currently executing command Esc Esc

Source

Description Windows & Linux Mac
Open document Ctrl+O Command+O
Save active document Ctrl+S Command+S
Run current line/selection Ctrl+Enter Command+Enter
Comment/uncomment current line/selection Ctrl+Shift+C Command+Shift+C
Find and Replace Ctrl+F Command+F

Editing (Console and Source)

Description Windows & Linux Mac
Undo Ctrl+Z Command+Z
Cut Ctrl+X Command+X
Copy Ctrl+C Command+C
Paste Ctrl+V Command+V
Select All Ctrl+A Command+A
Jump to Start/End Ctrl+Home/End Command+Up/Down
Select Shift+[Arrow] Shift+[Arrow]
Select to Line Start Alt+Shift+Left Command+Shift+Left
Select to Line End Alt+Shift+Right ommand+Shift+Right

Plots

Description Windows & Linux Mac
Previous plot Ctrl+Alt+F11 Command+Option+F11
Next plot Ctrl+Alt+F12 Command+Option+F12

Session

Description Windows & Linux Mac
Quit Session (desktop only) Ctrl+Q Command+Q
Restart R Session Ctrl+Shift+F10 Command+Shift+F10

For a complete list of Keyboard Shortcuts see the link

## 1.4 R Packages

A package is essentially a library of prewritten code designed to accomplish some task or a collection of tasks. For example, readr package is used to read various types of data files, survival package is used for survival analysis, ggplot2 is used for plotting and sp is for dealing with spatial data, etc.

R comes with a standard set of packages (including base, datasets, utils, grDevices, graphics, stats, and methods). They provide a wide range of functions and datasets that are available by default. Other packages are available for download and installation. Once installed, they must be loaded into the session in order to be used.

The command search() tells you which packages are loaded and ready to use.

It is important to remember that not all packages are of the same quality. Some are built to be very robust and are well-maintained, while others are built with good intentions but can fail with unforeseen errors, and others still are just plain poor.

Packages make up the backbone of the R community and experience. They are often considered what makes working with R so desirable. With such a large number of packages, finding the right one can be overwhelming. CRAN Task Views offers a curated listing of packages for different needs.

### 1.4.1 Installing R Packages

There are two ways of installing new packages: by typing commands in the R console or by using Rstudio interface.

Installing Packages using Commands

To Install a New Package follow these two steps:

• Step 1. ‘install.packages(“name of package”)’

• Step 2. ‘library(name of package)’

Example Installing readr package:

install.packages("readr")
library(readr)
When you install a new package following these steps R will remember this throughout your current session, but when you start a new session and want to use this package again you should first bring it to the current session by typing library(). For example, if you want to use readr package in a session you should first put it into the current session library by typing library(readr).

To check all the packages installed:

library()

search()

The function .libPaths() shows you where your library is located, and the function library() shows you what packages you’ve saved in your library.

You only need to install a package once. But like any software, packages are often updated by their authors. Use the command update.packages() to update any packages that you’ve installed. To see details on your packages, you can use the installed.packages() command. It lists the packages you have, along with their version numbers, dependencies, and other information.

Entering help(package="package_name") provides a brief description of the package and an index of the functions and datasets included.

Installing Packages using Rstudio

There is another way to install packages by usinf the GUI provided by RStudio. Note that in the window on the lower left corner there is a tab called Packages. Clicking on this tab you will see the list of packages already installed in R. If you want to put a package from the this list simply check out the box next to the package name.

If you want to install a new package, click the Install Packages button in the upper-left corner to bring up the dialog. From here simply type the name of a package (RStudio has a nice autocomplete feature for this) and click Install. Multiple packages can be specified, separated by commas. This downloads and installs the desired package, which is then available for use. Selecting the Install dependencies checkbox will automatically download and install all packages that the desired package requires to work.

### 1.4.2 Uninstalling and Unloading R Packages

In the rare instance when a package needs to be uninstalled, it is easiest to click the white X inside a gray circle on the right of the package descriptions in RStudio’s Packages pane. Alternatively, this can be done with remove.packages, where the first argument is a character vector naming the packages to be removed. For example, to remove the readr package

remove.packages("readr")

Sometimes a package needs to be unloaded. This is simple enough either by clearing the checkbox in RStudio’s Packages pane or by using the detach function in the R console:

detach("package:readr")

## 1.5 Learning R with swirl

swirl is an amazing interactive tool that let you learn and practice R right in the R console.

Let’s first install swirl and load it in the current session:

install.packages("swirl")
library("swirl")

Now you have the swirl in your current R session. To start working with swirl simply type swirl() into the console and hit Enter key:

swirl()

After that point swirl will walk you through your R adventure.

When you hit continue swirl is going to give you a bunhc of options (courses):

1: R Programming: The basics of programming in R
2: Regression Models: The basics of regression modeling in R
3: Statistical Inference: The basics of statistical inference in R
4: Exploratory Data Analysis: The basics of exploring data in R
5: Don't install anything for me. I'll do it myself.

Select 1 by typing 1 into the R console and then you will see the list of lessons under R Programming course:

 1: Basic Building Blocks  2: Workspace and Files  3: Sequences of Numbers
4: Vectors                5: Missing Values       6: Subsetting Vectors
7: Matrices and Data Fr   8: Logic                9: Functions
10: lapply and sapply     11: vapply and tapply   12: Looking at Data
13: Simulation            14: Dates and Times     15: Base Graphics           

Completing these lessons will give a solid working knowledge of R.

Here is a list of swirl commands that you might need when you are at the R prompt:

Command Description
... That’s your cue to press Enter to continue
Esc Exit swirl
skip() allows you to skip the current question.
play() lets you experiment with R on your own;
swirl will ignore what you do…
UNTIL you type nxt() which will regain swirl’s attention.
bye() causes swirl to exit. Your progress will be saved.
main() returns you to swirl’s main menu.
info() displays these options again.

## 1.6 Resources for R

Basics: A free interactive course covering the basics of R: Datacamp

Another one: Codeschool.

Basics + Stats: R Cookbook and Statmethods

Econometrics: Using R for Introductory Econometrics accompanies Wooldridge’s Introductory Econometrics by replicating the examples in R. You can read online and download all data sets and R files.

1. Here the purpose is to obtain an output that occupies two lines on your console, so if this doesn’t happen raise 40 to a larger integer.