class: center, middle, inverse, title-slide .title[ # POL 304: Using Data to Understand Politics and Society ] .subtitle[ ## Lab 1: Intro to R ] .author[ ### Olga Chyzh [www.olgachyzh.com] ] --- ## Always Start By... - Navigating to your working directory <img src="images/change_dir.jpg" width="1100px" style="display: block; margin: auto;" /> --- ## And ... - Setting your directory <img src="images/set_dir.jpg" width="1100px" style="display: block; margin: auto;" /> --- ## Open Data Files - To open `.csv` files, use `read.csv` ```r rosca<-read.csv('./data/rosca.csv', header=TRUE) ``` --- ## Access/Create a New Variable ```r rosca$bg_female rosca$new_var<- NA ``` --- ## Subset Rows by Value Suppose you want to see the values of the `fol2_amtinvest` variable only for women. ```r rosca$fol2_amtinvest[rosca$bg_female==1] ##Or create a copy of the dataset that only includes women: rosca1<- rosca$fol2_amtinvest[rosca$bg_female==1] ``` --- ## Calculate the Mean ```r mean(rosca$fol2_amtinvest, na.rm=TRUE) mean(rosca$fol2_amtinvest[rosca$bg_female==1], na.rm=TRUE) tapply(rosca$fol2_amtinvest, rosca$bg_female,mean, na.rm=TRUE) ``` --- ## Tabulate Observations by Category ```r table(rosca$bg_female) ``` --- ## Your Turn Create a single variable `treatment` that takes the value `control` for observations that received only encouragement, `safebox` if received a safe box, and `lockbox` if receiving a locked box. How many individuals are in the control group? How many individuals are in each of the treatment groups? --- ## Challenge Yourself - Run the code below to install the `qss-package` and `qss.student` packages that go with the textbook. ```r library(devtools) devtools::install_github("kosukeimai/qss-package", build_vignettes = TRUE) devtools::install_github("conjugateprior/qss.student") library(qss) library(qss.student) ``` - Run the code below to access the files that go with today's lab. This will put the files into a folder named "health-savings" in the directory you specified in 1. ```r get_pset("health-savings") ``` - Complete the problem set.