Practice

1. Rank Choice Voting Data

Here’s some example rank choice voting data with the candidates as columns and the individual voters as rows.

Reorganize this table so that you only have voter, candidate, and rank columns. Save it as a table called votes_long.

Now take this data and rearrange it so that individual voters are the columns and candidates are the rows.

Now take the votes_long data and use pivot_wider() to make a table with candidates as the columns and their vote total as a row.

Remember about the unique value issue. You’ll need to use select() to remove a column before pivoting.


2. SOmething

Here’s a table with fake weather data.

Rearrange this table so that there are columns for city, month, stat, measurement, and year.

Do the same thing for this table.

You should know how to do this if the year column can just contain 23, 24, or 25. But if you want the full 2023, 2024, 2025 you’ll need to use regular expressions. They’re tricky, but here’s a site that should help. See the next hint if you can’t get it.

To specify a number that is four digits you would use “[0-9]{4}” or “\\d{4}”. Also you could use “[a-z]+” to match any length of characters, so that would match both “temp” and “precip”.


3. City Data

Here’s a table with fake Oregon data.

Rearrange this table so that there is a column for “letter” that will have only the values a, b, or c and a column for “total”. Save this table as long_oregon.

Now rearrange the table so that each county has its own column with the totals in them.

Now use the documentation from pivot_wider() to create the same table but instead of having NA values, convert them to 0.