Exercise

Who won?

The iran data set contains all of the votes cast in this election, meaning that you can find the victor by computing the total number of votes for each candidate. In addition to answering the question of who won nationwide, you can also see how the results differed by province.

Along the way, you'll use a very useful function called gather(). This allows you to reshape your dataframe by taking information stored in variable names and move it be data that lives in it's own column.

Instructions

100 XP

Compute the total votes for each of the candidates by summarize()ing each of the candidate count columns with their sum. Name each new column with the name of the candidate and save the resulting dataframe as totals.

Gather the candidate names into a new column alongside their respective total votes. This step is done for you, but inspect the resulting dataframe to understand the structure. Use that dataframe to construct a bar plot to visualize the overall election outcome.

Did the candidate that came in second place overall beat the first place candidate in any of the provinces? Address this by grouping by province, summarizing with the sum of the votes of the top two candidates, and mutate a new TRUE/FALSE variable that is TRUE if the second place candidate has more votes. Finally, return a filtered dataframe that only contains the provinces where the second place candidate was more popular than the first place candidate.