install.packages('tinytex')
::install_tinytex() tinytex
Week 2: Writing Code
This week we introduce the statistical software that we will use to work with data this semester (R and RStudio). By the end of this week, you will learn:
How to write a script that loads a dataset and performs basic analyses
How to navigate file systems, create objects, and use functions
The basic structure of dataframes, units of analyses, types of variables
How to interpret summary statistics of single variables (mean, variance, standard deviation)
Reading
- DAFSS Chapter 1
Problem Set
This week’s problem set is designed to ensure that you have R and RStudio properly installed on your computer, and that you can write code that loads data, describes it, and performs a few basic analyses.
We will analyze a dataset from an experiment conducted in Michigan in 2006 (Gerber, Green, and Larimer 2008). In that experiment, voters were randomly assigned to receive a postcard with a “social pressure” message, describing the recipient’s voting history and the voting history of their neighbors. The dataset we will use is called voting.csv. The following table shows the names and descriptions of the variables in the dataset.
variable | description |
---|---|
birth | year of birth of registered voter |
message | whether the registered voter was assigned to receive the social pressure message: “yes” or “no” |
voted | whether registered voter voted in the August 2006 election: 1=voted, 0=didn’t vote |
Write an R script that performs the following analyses, in order. You can use comments in your script to respond to questions.
Download the voting.csv file (note: you can right click that link to save the file to your computer) and put it into your DSS folder. Read the dataset into R and save it as an object called
voting
.Use the function
head()
to view the first few observations of the dataset.How many observations are there in total? What is the unit of analysis?
What is the average value of the variable birth? Interpret this number.
What is the average value of the variable voted? Interpret this number.
Bonus. Install the
tinytex
package by entering the following lines of code into theR
console.1Once this is installed, you can compile your
R
script and its output as a PDF by clicking “Compile Report” in RStudio. Submit this PDF report to eLC.
Additional Resources
Teacup Giraffes: Intro To R
An interactive tutorial if you need additional help setting up R and RStudio
References
Footnotes
Not the
R
script! If you install a package in the script then it will reinstall the package every time you run the script, which is unnecessary and time-consuming.↩︎