Chapter 2 Introduction to R

Welcome to the wide world of R and RStudio! In this course you will be introduced - gently - into the world of computer programming. Increasingly, data analytics requires the use of specialized tools for analysis. In this course we will be using R and RStudio to analyze different sources of crime data. Before you get started, we will first become acquainted with the software. In this brief introduction I will be introducing a few important things to you:

  1. What is R and RStudio?
  2. How to use the RStudio environment.
  3. How to locate files and load data into R

Let’s get started!

2.1 What is R and RStudio?

R is, at its core, a statistical programming language. However, it is useful beyond simple statistics. Indeed, because it is a programming language it can be used for many different purposes. R is free and open-source, which means anyone can download and use it free of charge. Because of this, R is widely used in many academic and business circles.

RStudio is an integrated development environment (IDE) designed specifically for R. An IDE is simply a piece of software which makes it easier to work with a programming language. R is the programming language that runs under the hood, while RStudio is the piece of software which we will be working with primarily.

RStudio is an interface for the R language

Figure 2.1: RStudio is an interface for the R language

That being said, you should always be working in RStudio and not R. You have to install both, and both programs have an icon, but you only want to load RStudio!

Load RStudio, not R

Figure 2.2: Load RStudio, not R

2.2 How to navigate the RStudio environment

After opening RStudio you will likely be greeted with a window similar to this (although the colors may look different) on your computer. RStudio is made up of a number of window panes where you will be doing most of your work. The four most important parts are:

  1. The script window. This is where you will be writing your code and running commands.
  2. The environment tab. This is the spot where information about your data and variables will be stored.
  3. The console. This is where you will see output from your commands, as well as errors and warning messages.
  4. The plot window. This will store images you create in your analyses. You can also export images you create in lab.
RStudio interface and windows

Figure 2.3: RStudio interface and windows

Before you do anything, you will want to open a new script. A script is just a spot where you will write all your R code. To open a new script, you should navigate to File > New File > R Script and open a new script. This will fill the top-left of your RStudio window.

Creating a new script

Figure 2.4: Creating a new script

2.3 How to locate and load data

Loading data into R can often seem tricky. The important thing to know is that you just need to know how to point R to a file on your computer. If you type getwd() in the console window and hit ‘enter’ you should see a pop up showing you your username on your computer. In the next lab, you will need to know this in order to find a file on your desktop.

getwd()
## [1] "C:/Users/gioc4/Dropbox/crime_mapping_and_analysis"

This is called your working directory. Essentially, this is the default location R will try and look for files. You can also click on the ‘files’ tab under the plot window to interactively view files on your computer. You’ll see more of this in later labs.

Working directory and file addresses

Figure 2.5: Working directory and file addresses

Now that you’re familiar with the basics, you should move to Lab 1, where you will learn about installing packages, loading data, and doing some data analysis.