Get startedGet started for free

Using gather to tidy a dataset

In order to represent the joined vote-topic data in a tidy form so we can analyze and graph by topic, we need to transform the data so that each row has one combination of country-vote-topic. This will change the data from having six columns (me, nu, di, hr, co, ec) to having two columns (topic and has_topic).

This exercise is part of the course

Case Study: Exploratory Data Analysis in R

View Course

Exercise instructions

  • Load the tidyr package.
  • Gather the six topic columns in votes_joined into one column called topic (containing one of me, nu, etc.) and a column called has_topic (containing 0 or 1). Print the result without saving it.
  • You don't actually care about the cases where has_topic is 0. Perform the gather() operation again, but this time also filter for only the rows where the topic in topic describes the vote. Save the result as votes_gathered.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load the tidyr package


# Gather the six me/nu/di/hr/co/ec columns



# Perform gather again, then filter


Edit and Run Code