# Important to know

• This assignment is due in 1 week from announcement.
• Replace the author name with your name!
• You need to submit the RMD file before the due date. Do not submit HTML or PDF!

# Question 1 (10 points)

Your first task is to load the data into R. You are NOT allowed to change the XLS file in any way. All grading tests will be run on the original XLS file. If you assume a changed XLS file, than you risk losing full grades for the whole assignment.

# Load the excel data into R object rawData
rawData <- read_excel("secim.xlsx", range = "C1:W259", col_names = TRUE)

# Read and process the city names from the Excel file
cities <- read_excel("secim.xlsx", range = "A2:A259", col_names = FALSE)$X__1 for (i in 1:length(cities)) if (is.na(cities[i])) { if (!is.na(cities[i - 1])) cities[i] <- cities[i - 1] else cities[i] <- cities[i - 2] } type <- rep(c("Toplam", "Yurt içi", "Yurt Dışı"), length.out = length(cities)) rawData <- cbind(rawData, Cevre = cities, Tip = type) rawData <- rawData[, c(ncol(rawData) - 1, ncol(rawData), 1:(ncol(rawData) - 2))] str(rawData) # Question 2 (20 points) Plot the election results (vote distribution as percentages among parties) for all votes. Your plot must display the parties in descending sorted order. ## Your Answer vec <- as.matrix(rawData[1, 7:23]) / rawData$Geçerli oy sayısı * 100
ord <- order(vec, decreasing = T)
vec <- vec[, ord]
barplot(vec,
cex.names = 0.5,
las = 2,
main = "Türkiye Geneli Seçim Sonuçları",
ylim = c(0, 60),
ylab = "Yüzde Oy Oranı")

# Question 3 (20 points)

This time plot the percentages for “In country” and “Off Country” votes. Place them on the same plot, side by side to be able to see how parties are affected.

vec <- as.matrix(rawData[2:3, 7:23]) / rawData$Geçerli oy sayısı[2:3] * 100 ord <- order(vec[1,], decreasing = T) vec <- vec[, ord] barplot(vec, cex.names = 0.5, las = 2, main = "Yurt İçi / Yurt Dışı Oy Oranları", ylim = c(0, 60), ylab = "Yüzde Oy Oranı", beside = TRUE) # Question 4 (30 points) Draw a plot of each party’s number of winnings, i.e. the number of regions where they got the most votes among all parties. Only consider the total votes’ rows. ## Your Answer # Prepare the data for this question qData <- as.matrix(rawData[(1:85) * 3 + 1, 7:23]) # Determine the winner in each region winner <- numeric() for (i in 1:nrow(qData)) { winner[i] <- which.max(qData[i, ]) } # Count the number of winnings for each party winCounts <- numeric() for (i in 1:ncol(qData)) { winCounts[i] <- sum(winner == i) } names(winCounts) <- colnames(qData) winCounts <- sort(winCounts, decreasing = T) # Do the plot: apply a barplot for best exposition barplot(winCounts, ylim = c(0, 75), xlab = "Parti", ylab = "Kazanılan bölge", main = "Partilerin kazandığı bölge sayıları", las = 2, cex.names = 0.5) # Question 5 (30 points) In this question you will be comparing the performances of top 4 parties, head to head. • For each one of the top four parties: • Draw a scatterplot of their vote percentages vs each of the other parties’ vote percentages, with respect to each region • Use the party names as x and y labels • Draw a line with parameters a as 0, and b as 1, color it blue, and make it 2 points wide • Draw a line with parameters a as 0, and b as .5, color it green, and make it 2 points wide • Draw a line with parameters a as 0, and b as 2, color it red, and make it 2 points wide • Draw a line with parameters a as 20, and b as -1, color it orange, and make it 2 points wide • All three plots for each party must be placed on a single plot • Comment on the plots from an exploratory perspective. What do these plots tell you? ## Your Answer wdf <- rawData[rawData$Tip == "Toplam", c("AK PARTİ", "CHP", "MHP", "HDP")] / rawData[rawData\$Tip == "Toplam", "Geçerli oy sayısı"] * 100
par(mfrow = c(1, 3))

for (i in 1:4)
for (j in 1:4)
if (i != j)
{
plot(wdf[[i]], wdf[[j]],
xlab = colnames(wdf)[i], ylab = colnames(wdf)[j])
abline(a = 0, b = 1, col = "blue", lwd = 2)
abline(a = 0, b = .5, col = "green", lwd = 2)
abline(a = 0, b = 2, col = "red", lwd = 2)
abline(a = 20, b = -1, col = "orange", lwd = 2)
}