library(tidyverse)
library(socviz)
library(juanr)
library(gtools)
library(gapminder)
library(here)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/usingggsave()andhere() - Save any cleaned data in
data/clean/. Never overwrite anything indata/ - 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.
Using
leader, use subsetting to identify the following:A Vietnamese Emperor who, in his first year in office, was 11 years old. Famously depraved.
Leaders with graduate degrees who in 2015 reached their 16th year in power.
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.
Take the
moviesdataset 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?Look before you compute: run
summary()on thebudgetcolumn. Some movies have a budget under $10,000. Usefilter()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.Add a column for
profitthat equals the difference between how much the movie grossed and its budget. What unit is it in?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?
Take any existing dataset you like from the
juanrpackage and do the following (list here):Filter the data to a subset of your choosing.
Create a new variable that is a transformation of an existing variable (a rate, a per-capita number, a difference, a percent).
Save the data in the
data/clean/folder as a .csv, usinghere()for the pathOpen the .csv in Excel or equivalent software. Then restart R and run your whole script from the top