If i have a data set similar to the following:
# State Ben.Carson.Number.of.Votes Ben.Carson.Party Ben.Carson.Percent Bernie.Sanders.Votes Bernie Sanders.Percent Bernie.Sanders.Party
# OH 305. Republican 8.3 500 12.30 Democrat
# FL 20 Republican 3.0 700 11.00. Democrat
# TX 400. Republican 5.0 50 1.00 Democrat
How do I create four unified columns, Candidate Name, Votes, Percent, and Party, from all the separate columns located in the data set currently? I.e gather together all three types of columns based on the candidate name located in the column name.
I tried the following but to no avail:
tidyElectionData %>%
gather(key, value, -c(County, Location.State, State)) %>%
separate(key, into = c("Candidate", "Party"), sep = "(^[^.]+[.][^.]+)(.+$)") %>%
spread(Party, value)