Analyzing election results

First, download this .csv file from the New York Times: 2016_election_results_nyt.csv

Step 1: import the CSV into Google Sheets

A spreadsheet on Google Sheets containing election results

Step 2: move the header row down. Why? Because it’s good practice, showcases proficiency with Google Sheets, and means it’s easier to see what rows you’re in when scrolled down.

Spreadsheet after moving header row down

Step 3: click on cell E2. We’re going to do some math to create a new column.

Spreadsheet with cell E2 selected

Step 4: type the following into the cell. =A2+B2

Spreadsheet with '=A2+B2' typed into cell E2

Step 5: press enter to see your newly minted formula compute.

Spreadsheet with the result of '=A2+B2' visible

Step 6: we have applied this formula to a single row. Now we want to apply it to all rows. Click on E2 again.

Spreadsheet with E2 selected after calculating result

Delicately position the mouse so that the cursor is over the small blue square in the bottom right of cell E2. The cursor should turn into a plus symbol.

Spreadsheet with the bottom right corner of E2 hovered over

Now, while the cursor looks like the plus symbol, double-click. This will extend the formula to all rows!

Spreadsheet with the formula E applied to all rows

Step 7: click on cell E1 and type to give the header a meaningful name, like “Sum of votes.”

Spreadsheet with column E renamed 'Sum of votes'

Fun with math

Wow, we’ve just made a formula. It may not seem like much, but we ran an algorithm and applied it to a lot of data at once.

Let’s have some fun with it.

We’re going to create some more columns just like “Sum of votes”. If you need more room in your spreadsheet, select the last column, go to the menu and hit Insert > Column Right. This will insert a column to the right of the currently selected one. You can repeat this action multiple times to create more space for yourself.

Spreadsheet with the menu item selected to insert a column to the right

Step 8: click on cell F2 and type the formula =B2-A2. Note: starting a cell with = makes it into a formula.

Spreadsheet with '=B2-A2' typed into cell F2

Step 9: following the same instructions as before, compute this formula and apply it to all rows. Then rename the column something like “Trump margin” to show that this column captures how many votes Trump had over Clinton in each place.

Spreadsheet with column F populated with a formula and renamed 'Trump margin'

Fun with filters

Step 10: let’s see which place had the largest margin of Trump voters. To accomplish this, we are going to sort the Trump margin column. We will create a filter to safely sort the data without altering it.

First, select cell A1 so that Google Sheets knows to filter the right data (if you have a cell selected outside your data, it won’t work).

Cell A1 selected

After selecting cell A1, go into the menu and click Data > Create Filter.

The Data menu open with 'Create Filter' highlighted

After applying the filter, your spreadsheet should look like a sea of green.

The green filter look that results from applying the filter

But the cells are all selected too, causing the cells to look blue. Let’s clear the selection to make it easier to work with the data. Click on cell A1 again to erase the selection.

The green filter look but after clicking A1 again to deselect it

Step 11: now that we’ve created a filter view, the data is green and there’s an icon next to each column that looks like an inverted pyramid. Click on the pyramid icon for the “Trump margin” column.

Hovering over the filter icon in the 'Trump margin' column

The filter menu should pop up for the “Trump margin” column, showing us its values and sorting options.

Expanding the filter in the 'Trump margin' column

Let’s sort the column to see which county has the largest margin. To sort by largest first, click on Sort A → Z

Hovering over the 'Sort A -> Z' menu item in the filter

Voila! The results are sorted. And we can see that Montgomery, Texas has the highest margin for Trump.

The results of sorting the margin column

Step 12: We’re in Pennsylvania, so we are interested in data about Pennsylvania. Let’s filter down the state column to only show data for Pennsylvania.

Click the inverted pyramid in the “State” column to open the filter menu for state.

Expanded the filter options for the 'State' column

Now we want to only show the state called “Pennsylvania” — the states with checkmarks next to them show all the values that appear in that column. To only show “Pennsylvania” we could manually uncheck all of them except for “Pennsylvania,” but that would take a while. Instead, let’s erase all the states and then add only “Pennsylvania” back in.

Click on the Clear button to erase the checkmarks next to everything.

Hovering over the clear button in the filter

Now, all the state values are unchecked. Let’s find “Pennsylvania.” We could scroll down, or just type “Pennsylvania” into the search box.

As we type, notice the results filter down until all that remains is “Pennsylvania.”

Typing 'Pennsylvania' to filter the states down

Now, just click on the value for “Pennsylvania” below the search box to cause a check mark to appear.

Clicking on 'Pennsylvania' to put a check mark there

Hit Ok to apply the filter and see the results.

Applying the filter to get results for only the state of 'Pennsylvania'