#
# Code for lab on exit poll data
#


## PREP STEP 1
## Open Ch2_Lab_ExitPoll.R in script window of RStudio
# a) Open RStudio
# a) Go to File/New Project - select New Directory
# b) Pick New Project and pick a directory name (such as "IntroR") for the
#    subdirectory of your choice (such as "C:/Users/baileyma/Documents/")
# c) Save all files for lab to that subdirectory (the *.dta and *.R are most important)
# d) In RStudio, go to File/Open File and browse to the directory you created and saved the *.R file to.
# e) Open the file "Ch2_Lab_ExitPoll.R". (It may be named "Ch2_Lab_ExitPoll.R.txt" - that's fine; just save w/o the .txt at the end.)

## PREP STEP 2

## Load data using the following code.
## Highlight the code and "run" (command + enter on Mac; control + enter on Windows)
library(readxl)
dta <- read_excel("Ch2_Lab_ExitPoll.xlsx")

# Check path - this is where R will look for files
getwd()

# Show files in your directory listed at getwd()
dir()

## PREP STEP 3
## Check out data
objects()
names(dta)

# First 4 rows and first 10 columns
dta[1:4, 1:10]

# First 4 rows and columns that have feeling thermometers
dta[1:4, 49:58]


#
# EXERCISES
#

# 2) Create dummy variables for DC, Maryland, Virginia and Ohio.
# Codebook excerpt: dta$state:  1 for DC, 2 for MD, 3 for OH and 4 for VA

# Example
dta$DC        <- (dta$state == 1)
table(dta$DC)

# How many observations are in each state?


# 3)	Create an age variable using the dta$year_born variable.
#     Check for and *correct* data errors.  
#     What is the average age of all observations in the data set?  
#     The minimum and maximum?



# 4)	What is the distribution of the gender variable?  
# Codebook excerpt: dta$gender: 1 for male, 2 for female, 3 for other
#     Create a male dummy variable and indicate the distribution of this variable. 
#     Compare distribution of the male variable to the distribution of the gender variable.



# 5)	Provide descriptive stats for Trump and Clinton feeling thermometer.
# Codebook excerpt: therm_clinton, therm_trump
#     Is there anything you need to adjust?



# 6) Produce histograms of the Clinton and Trump feeling thermometers


# 7) Create a scatterplot of the Clinton and Trump feeling thermometers


# 8)	What is the distribution of the education variable?
#     Can you use this as a continuous variable in a regression model?

# Codebook excerpt: dta$education ("Q41 37. What is the highest level of education you have completed?")
# Some high school (1)
# High school diploma/GED (2)
# Some college (3)
# Other (4)
# 2 year college degree (5)
# 4 year collage degree (6)
# Post-graduate degree (7)

