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('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 - Complete the practice problem set posted on the course website.