R Tutorial: The United Nations Voting Dataset

Dave Robinson teaches R analysis of UN voting data with 508,000 roll-call votes. He shows how to load the dataset, filter votes to yes/abstain/no, and use dplyr pipes to create a year column and add country names. The course covers dplyr and ggplot2 for exploring historical voting patterns.
Chapters
Hi, I'm Dave Robinson and I'll be your instructor for this course. I'm a data scientist and I really enjoy using R to dive into a data set and discover interesting things. In this course we're going to be using some of my favorite R packages such as dplyr and ggplot2 to explore and draw conclusions from a real world data set. If you've used these packages before, this will be a great opportunity to practice using them in an analysis Lets introduce the data set which contains the historical voting data from the General assembly of the United Nations. In the General assembly, every member nation gets a vote, which makes this a great opportunity to explore the history of international relations. In our data analysis vocabulary rows of a data set are called observations and columns are called variables. In this data set, each observation represents one combination of a roll call vote and a country. The first variable, rcid, is the roll call ID describing one round of voting, such as to approve a United nations resolution. The session variable represents which year long session in the UN's history the vote was cast. Note that to keep the data set at a reasonable size or only sessions from alternating years are included. The vote column represents that country's choice. For example, 1 means a yes vote and 9 means a country was not yet a member of the United Nations. The C code column is a country code that uniquely specifies the country. To work with this in R, we'd start by loading the dplyr package, which offers tools for manipulating data. Then we can view the votes data set by simply typing votes into the R prompt. Here you can see each of the columns of the table, as well as the table size 508,000 rows. As with almost any data set you'll run into, you need to clean this data before we can start analyzing it. Let's review one of the most important tools for performing multiple sequential steps on data the pipe operator. The pipe typed as percent percent tells R to pass an object in as the first argument of the next function, which lets us perform multiple operations in a series. While it may seem complicated if you haven't used it much before, you'll quickly get comfortable with it. The operations we'll usually be composing are dplyr's verbs, functions that perform a single simple action on a data set. For example, the filter verb subsets observations from a data set to remove rows that aren't interesting to us. The mutate verb adds a variable or changes an existing variable. Here's an example of each in our original dataset, the vote column has five possible one for yes, two for abstain, three for no, eight meaning the country wasn't present, and nine meaning the country was not yet a member. We only care about the first three values, yes, no, and abstain. To remove the others, we pipe the data set into the filter function. Within that filter, we describe a condition Vote is less than or equal to 3. The resulting data frame is smaller. It only kept the observations where our condition was met. You will also be using the mutate function. The session variable is hard to interpret, but if you know that the first session of the United nations was held in 1946, you can use it to get the year each vote was cast, which is much more interpretable. To do this, you could pipe the data into the mutate function, where you can define your New year column as 1945 the Session. Notice the New Year column with the result in your exercises. You'll also clean up the country column to include full country names instead of IDs. The pipe operator lets you chain these simple actions together in a sequence. You'll get into the habit of piping many small, simple operations together to perform a richer analysis.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







