It looks like this code is trying to find the winners of an election in each year, state, and district, only including those who won by a margin of 5% or less.
Im trying to create a code that finds the winners an election in each year, state and district, only including those who won by a margin of 5% or less. I have created the following code but repeatedly get error, and i cant seem to understand why. Can someone please help me understand what im doing wrong? :)
tab_relevant %>%
group_by(year, state, district) %>%
{
if (tab_relevant$candidatevotes/tab_relevant$totalvotes <= 0.05) {
slice_max(tab_relevant$candidatevotes)
} else {
return(FALSE)
}
} -> winner