Homework 2

Due Date: Wednesday, 28 February by 23:59:59

Submission Instructions: Submit your completed R script to the corresponding Moodle assignment. Your file should contain a header and the file name should be formatted according to the guidelines discussed in class and posted in the course slides.

Be sure to show your work. This means that your answer to each question should show the code you used to obtain the answer. You do not need to show other steps that may have been taken to get the answer (e.g., trial and error), only the code that provides the answer.

Question #1:

Part A:

Using the socviz package, load the GSS data into an object called gss. Use the table() function and subset operator to find the number of respondents by agegrp.

In a comment, identify the number of respondents between the ages of 35-45.

Part B:

Next, use the dplyr functions to output a tibble which summarizes the number of respondents by agegrp.

In a comment, identify the number of respondents between the ages of 45-55.

Part C:

Output another tibble which summarizes respondents by agegrp and whether they voted for obama in the 2012 U.S. presidential election. In this tibble, include both the count (call it total), relative frequency (freq), and percentage (pct). You do not have to round the percentages but can if you wish.

In a comment, identify the number of respondents between the ages of 18-35 who voted for Obama. In a separate comment, identify the number of respondents between the ages of 18-35 that have missing values for the obama variable.

Question #2:

Again using the GSS data from the socviz package, create a tibble called marit_happy which summarizes the happiness of GSS respondents by marital status. Filter your data so that it shows only respondents who are “Married” or “Never Married”.

In a comment consisting of a few brief sentences, compare the reported happiness of respondents who are married to those who have never been married.1

Question #3:

Also using the same GSS data, output a tibble with: (1) the number of respondents by degree (i.e., the respondent’s highest degree level) and (2) the mean number of children by degree. Hint: you may need to use an na.rm argument somewhere.

In a brief comment, identify the relationship between degree-level and number of children among respondents.


  1. A brief technical note: although the GSS is a nationally representative survey of U.S. adults, we are using the term “respondents” throughout this assignment rather than “U.S. adults.”

    The reason for this is that estimating population-level statistics (like the proportion of U.S. adults who are married) requires using survey weights, which is a slightly more complicated procedure that takes into account the survey design. Survey weights are used to help ensure that the statistics being reported from survey data accurately reflect the population.

    In practice, the numbers you will obtain in this assignment are very close to the best estimates possible for U.S. adults, but since they’re not exactly precise (i.e., they don’t take into account the survey weights), it’s more accurate to refer to your results as relating to the respondents of the GSS rather than all U.S. adults. The differences between the properly weighted results and the results you obtain in this assignment are within 1%, however.↩︎