Can anyone give me a hint as to how I can appropriately assign the votes to each candidate using the if and if-else statement? The commands below are counting the entire sum of the votes and I can't seem to figure out how to make R separate the votes for each candidate (Trump and Biden). Sorry for such a newbie question...
# Read CSV file
results <- read.csv("PRESIDENT_precinct_primary.csv")
View(results)
# Votes set to zero
Trump <- 0
Biden <- 0
n <- length(results)
for(i in 1:n){
if(results$candidate[i] == "DONALD J TRUMP"){
Trump <- sum(results$votes[i])
}
else if(results$candidate[i] == "JOSEPH R BIDEN"){
Biden <- sum(results$votes[i])
}
else{}
}
# Final Results
cat("Trump:", Trump)
cat("Biden:", Biden)
Aucun commentaire:
Enregistrer un commentaire