Problem set: data wrangling

Overview

Inside your camp project, write a script called code/02_set.R that answers the questions below. It is your script: for any line in it, you should be able to say what it does. New today: for every filter(), say how many rows it dropped; for every mutate(), say what unit the new column is in; for every =, say what is in the object.

For all of your questions, do the following:

  • Make the graphs look good (add labels, a theme)
  • Make your code look good (comment it, write it in chunks)
  • Save each of your plots in figures/ using ggsave() and here()
  • Save any cleaned data in data/clean/. Never overwrite anything in data/
  • When you think you’re done: restart R and run the script top to bottom, from a fresh session. If it works then, it works

Nothing is turned in. Your figures/ and data/clean/ folders are the record of what you did.

Questions

You’ll need the following packages. If gtools is not installed yet, run install.packages("gtools") once in the Console first.

library(tidyverse)
library(socviz)
library(juanr)
library(gtools)
library(gapminder)
library(here)
  1. Using leader, use subsetting to identify the following:

    1. A Vietnamese Emperor who, in his first year in office, was 11 years old. Famously depraved.

    2. Leaders with graduate degrees who in 2015 reached their 16th year in power.

  2. Copy the code chunk below to generate the data object fake_parties, which provides vote shares for five parties in five imaginary elections.

fake_parties = rdirichlet(n = 5, alpha = c(1, .8, .3, 1, .2)) |> 
  as_tibble() |> 
  rename("party_1" = 1,"party_2" = 2, "party_3" = 3, "party_4" = 4, "party_5" = 5) |> 
  mutate(election_round = 1:5) |> 
  select(election_round, everything())

Electoral scholars often want to know how many parties are effectively competing in an election. For example, although 1200 people ran for US president in 2020, including candidates from the Green Party and Libertarian Party, we would say there are effectively only two parties in the US. Laakso and Taagepera (1979) came up with a metric to measure this idea, using this formula:

\[ N={\frac {1}{\sum _{i=1}^{n}p_{i}^{2}}}\]

Where \(p_i\) is party \(i\) share of the vote in an election and \(N\) is the effective number of parties. Write code that calculates the Laakso and Taagepera (1979) effective number of parties metric for each election in fake_parties.

  1. Take the movies dataset and subset it so you are only looking at movies made after the year 1950. Also subset so you are only looking at movies where the primary genre (genre1) is action, horror, comedy, or drama. Save the result under a new name. How many rows did you drop?

    1. Look before you compute: run summary() on the budget column. Some movies have a budget under $10,000. Use filter() to look at those rows. Are they data errors, or real? Decide what to do with them (keep or drop), do it, and say how many rows it changed.

    2. Add a column for profit that equals the difference between how much the movie grossed and its budget. What unit is it in?

    3. Save your data in the data/clean/ folder. Then make a histogram of profit, and a second histogram where the four genres are separated by fill. What do you see?

  2. Take any existing dataset you like from the juanr package and do the following (list here):

    1. Filter the data to a subset of your choosing.

    2. Create a new variable that is a transformation of an existing variable (a rate, a per-capita number, a difference, a percent).

    3. Save the data in the data/clean/ folder as a .csv, using here() for the path

    4. Open the .csv in Excel or equivalent software. Then restart R and run your whole script from the top

References

Laakso, Markku, and Rein Taagepera. 1979. “‘Effective’ Number of Parties: A Measure with Application to West Europe.” Comparative Political Studies 12 (1): 3–27.