+ - 0:00:00
Notes for current slide
Notes for next slide

POL 478H1 F

Intro to R

Olga Chyzh [www.olgachyzh.com]

1 / 10

Outline

  • Input data

  • Look at data

  • Extract pieces

  • Basic graphing in ggplot2

2 / 10

Course Data

  • Many commands/features in R are a part of a specific package.

  • You must install and load the package in order to use these commands/features.

  • We have an R package for this class. To install/update, copy and paste the following code into your RStudio source editor and run.

library(devtools)
devtools::install_github("ochyzh/classdata")
  • To load the package (at the beginning of each R session if you plan to use it):
library(classdata)
3 / 10

Getting Help Within R

If you want to learn about a specific command:

?command
help("command")
help.search("command")
??command
4 / 10

R Reference Card

5 / 10

Your Turn (5 min)

  • Install the package classdata on your computer

  • Load the package into your current R session:

library(classdata)
  • Look at the R help for the dataset terr_attacks

  • What happens if you just type in the name of the dataset?

6 / 10

Exploring Objects

For any R object x, we can use any of the following:

  • x

  • head(x)

  • summary(x)

  • str(x)

  • dim(x)

Try these commands on the terr_attacks data.

7 / 10

str is for structure

data(terr_attacks)
str(terr_attacks)
## 'data.frame': 16120 obs. of 10 variables:
## $ country : chr "Afghanistan" "Albania" "Algeria" "Angola" ...
## $ ccode : num 700 339 615 540 160 371 900 305 373 692 ...
## $ cabb : chr "AFG" "ALB" "DZA" "AGO" ...
## $ year : int 2001 2001 2001 2001 2001 2001 2001 2001 2001 2001 ...
## $ type : chr "Armed Assault" "Armed Assault" "Armed Assault" "Armed Assault" ...
## $ num_attacks: num 2 0 80 22 0 0 0 0 2 0 ...
## $ GDPpc : num NA 2454 3617 2214 7776 ...
## $ population : num 20531160 3060173 31590320 15562791 37471535 ...
## $ tradeofgdp : num NA 57.4 58.7 150.3 21.9 ...
## $ polity2 : int NA 5 -3 -3 8 5 10 10 -7 -8 ...
8 / 10

Extract Parts of an Object

  • x$variable

  • x[,"variable"]

  • x[rows, columns]

  • x$variable[rows]

rows and columns are vectors or indices.

Try these commands on the terr_attacks data.

9 / 10

Your Turn

  • Look at the first 10 rows of the terr_attacks data

  • Why are some values coded as NA? Look at the help for NA.

10 / 10

Outline

  • Input data

  • Look at data

  • Extract pieces

  • Basic graphing in ggplot2

2 / 10
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow