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
Exercise instructions
- Load the
tidyrpackage. - Gather the six topic columns in
votes_joinedinto one column calledtopic(containing one ofme,nu, etc.) and a column calledhas_topic(containing 0 or 1). Print the result without saving it. - You don't actually care about the cases where
has_topicis 0. Perform thegather()operation again, but this time also filter for only the rows where the topic intopicdescribes the vote. Save the result asvotes_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