Our purpose is to analyse the results of 2019 European Parliament election in Italy by using a Pivot Table. We focus our attention to the 4 provinces of Abruzzo region.

setwd("~/Coursera/Data_Science")
fileloc <- "./datasets/Italia/europee2019_scrutini_area_italia.csv"
itdata <- read.csv(fileloc, header = TRUE, sep = ";", 
                    strip.white = TRUE, stringsAsFactors = FALSE)
names(itdata)[c(3,4,5,6)] <- c("Provincia", "Comune", "Lista", "Voti")

You can also embed plots, for example:

abruzzo <- itdata[itdata$REGIONE == "ABRUZZO", -1]
head(abruzzo, 5)
##       REGIONE Provincia Comune                Lista Voti
## 86251 ABRUZZO    CHIETI ALTINO LEGA SALVINI PREMIER  618
## 86252 ABRUZZO    CHIETI ALTINO   MOVIMENTO 5 STELLE  413
## 86253 ABRUZZO    CHIETI ALTINO  PARTITO DEMOCRATICO  232
## 86254 ABRUZZO    CHIETI ALTINO         FORZA ITALIA  183
## 86255 ABRUZZO    CHIETI ALTINO    FRATELLI D'ITALIA   80
## we are interested to the 5 main parties
parties <- head(abruzzo$Lista, 5)
abruzzo$Party <- ifelse(test = abruzzo$Lista %in% parties, yes = abruzzo$Lista, no = "Altro")

Use a Pivot Table to display the vote percentages of the 5 parties by province:

library(rpivotTable)
rpivotTable(data = abruzzo, rows = "Provincia", cols = "Party", aggregatorName = "Sum as Fraction of Rows", 
            vals = "Voti", rendererName = "Heatmap")