Week 1: R Overview

POP88162 Introduction to Quantitative Research Methods

Tom Paskhalis

Department of Political Science, Trinity College Dublin

Topics for Today

  • History of R
  • R operators and operations
  • R objects and assignment
  • Scripts and R Markdown

R Background

  • S (for statistics) is a programming language for statistical analysis developed in 1976 in AT&T Bell Labs.
  • Original S language and its extension S-PLUS were closed source.
  • In 1991 Ross Ihaka and Robert Gentleman began developing R, an open-source alternative to S.

R Basics

  • R is an free and open-source programming language
  • With a focus on statistical analysis
  • R is often used for interactive data analysis (one command at a time)
  • But it also permits to execute entire scripts in batch mode
  • Example R code:
2 + 3
[1] 5

Operators

Key operators in R are:

  • Arithmetic (+, -, *, ^, /, %/%, %%, %*%)
  • Boolean (&, &&, |, ||, !)
  • Relational (==, !=, >, >=, <, <=)
  • Assignment (<-, <<-, =)
  • Membership (%in%)

Mathematical Operations

Arithmetic operations in R:

1 + 1
[1] 2
5 - 3
[1] 2
6 / 2
[1] 3
4 * 4
[1] 16

Scientific Notation

\(5^2 = 5 \times 5 = 25\)

5 ^ 2
[1] 25

\(5^{-2} = \frac{1}{5^2} = \frac{1}{5 \times 5} = \frac{1}{25} = 0.04\)

5 ^ -2
[1] 0.04

\(5 \times 10^{-2} = 5 \times \frac{1}{10^2} = 5 \times \frac{1}{100} = \frac{5}{100} = 0.05\)

5 * 10 ^ -2
[1] 0.05

\(5 \times 10^{-3} = \frac{5}{1000} = 0.005\)

5 * 10 ^ -3
[1] 0.005

\(5 \times 10^{-4} = \frac{5}{10000} = 0.0005\)

5 * 10 ^ -4
[1] 5e-04

Logical Operations

3 != 1 # Not equal
[1] TRUE
3 > 3 # Greater than
[1] FALSE
# True if either first OR second operand is True, False otherwise
FALSE | TRUE
[1] TRUE
# R also treats F and T as Boolean, but
# it is not recommended due to poor legibility
F | T
[1] TRUE
# True if both first AND second operand are True, False otherwise
FALSE & TRUE
[1] FALSE
# Combining 3 Boolean expressions
3 > 3 | 3 >= 3
[1] TRUE

R Objects

Everything that exists in R is an object.

John Chambers

  • Fundamentally, everything you are dealing with in R is an object.
  • That includes individual variables, datasets, functions and many other classes of objects.
  • The key reference to an object is its name.
  • Typically, the reference is established through assignment operation.

Assignment Operations

  • <- is the standard assignment operator in R.
  • While = is also supported it is not recommended.
x <- 3
x
[1] 3
census <- read.csv("./data/census.csv")

R and Development Environments

  • There is some choice of integrated development environments (IDEs) for R (StatET, ESS, R Commander).
  • However, over the last decade RStudio became the de factor standard IDE for working in R.
  • You can also find R extensions for your favourite text editor (Sublime Text, Visual Studio Code, Vim).

RStudio

R Script

  • Usually you want to have a record of what analysis was done and how you did it.
  • So, instead of writing all your R commands in the interactive console,
  • You can create an R script, write them there and run then together or one at a time.
  • R script is a file with .R extension and contains a collection of valid R commands.

R Markdown

  • Markdown:
    • Easy-to-read and easy-to-write plain text format;
    • Separates content from its appearance (rendition);
    • Widely used across industry sectors and academic fields;
    • .md file extension.
  • RMarkdown:
    • Allows combining of R commands with regular text;
    • Compiles into PDF/DOC/HTML and other formats;
    • Can be converted into slide deck or even website!
    • .Rmd file extension
  • All R assignments must be submitted in PDF/DOC files compiled from R Markdown!

Markdown Formatting Basics

  • Use _ or * for emphasis (single - italic, double - bold, triple - bold and italic)
    • *one* becomes one, __two__ - two and ***three*** - three
  • Headers or decreasing levels follow #, ##, ###, #### and so on
  • (Unordered) Lists follow marker -, + or *
    • Start at the left-most position for top-level
    • Indent four space and use another marker for nesting like here
  • (Numbered) Lists use 1. (counter is auto-incremented)
  • Links have syntax of [some text here](url_here)
  • Images similarly: ![alt text](url or path to image)

R Markdown Example

R Markdown

Rendering

### Title

Some text in *italic* and **bold**

Simple list:

- A
- B

Ordered list:

1. A
1. B

Example, where $Y_i = 5 + X_i + \epsilon$

```{r}
x_i <- 3
epsilon <- rnorm(1)
y_i <- 5 + x_i + epsilon
y_i
```

Title

Some text in italic and bold

Simple list:

  • A
  • B

Ordered list:

  1. A
  2. B

Example, where \(Y_i = 5 + X_i + \epsilon\)

x_i <- 3
epsilon <- rnorm(1)
y_i <- 5 + x_i + epsilon
y_i
[1] 7.250057

Next

  • Tutorial:
    • Getting started with R
  • Next week:
    • Data Structures