class: center, middle, inverse, title-slide # POL 478H1 F ## Intro to R ### Olga Chyzh [www.olgachyzh.com] --- ## Outline - Input data - Look at data - Extract pieces - Basic graphing in `ggplot2` --- ## 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. ```r library(devtools) devtools::install_github("ochyzh/classdata") ``` - To load the package (at the beginning of each R session if you plan to use it): ```r library(classdata) ``` --- #Getting Help Within R If you want to learn about a specific command: ```r ?command help("command") help.search("command") ??command ``` --- ## R Reference Card - http://cran.r-project.org/doc/contrib/Short-refcard.pdf - Save/print it to reference as you work. --- ## Your Turn (5 min) - Install the package `classdata` on your computer - Load the package into your current R session: ```r library(classdata) ``` - Look at the R help for the dataset `terr_attacks` - What happens if you just type in the name of the dataset? --- ## 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. --- ## `str` is for *structure* ```r 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 ... ``` --- ## 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. --- ## 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`.