Created
June 20, 2012 16:27
-
-
Save datagistips/2960781 to your computer and use it in GitHub Desktop.
Fichier des gagnants aux élections législatives 2nd Tour d'après le fichier de Data Publica
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(gdata) | |
library(stringr) | |
#Fichier source : http://www.data-publica.com/data/13622--resultat-par-circonscription-du-2nd-tour-des-elections-legislatives-17-juin-2012 | |
res <- read.xls("IN/Leg 2012 Résultats CirLG FE T2.xls") # résultats législatifs | |
########################### | |
# RECUPERATION DU GAGNANT # | |
########################### | |
# PRE-TRAITEMENT | |
dfBase <- res[ ,1:16] | |
resL <- list() | |
resL[[1]] <- res[,17:24] | |
resL[[2]] <- res[,25:32] | |
resL[[3]] <- res[,33:40] | |
for (i in 1:3) { | |
nms <- c() | |
names(resL[[i]]) <- names(resL[[1]]) | |
} | |
# RECUPERATION DU GAGNANT | |
wch <- sapply(1:nrow(res), function(x) which.max(sapply(1:3, function(i) resL[[i]][x, 6]))) | |
rowSel <- vector(mode="list") | |
rowSel <- lapply(1:nrow(res), function(x) resL[[wch[x]]][x, ]) | |
dfSel <- do.call("rbind", rowSel) | |
# ADJONCTION AVEC DONNEES GENERALES | |
df <- cbind(dfBase, dfSel) | |
# NUANCES LIBELLEES | |
nuances <- read.xls("F:/METHODES/1205_LEGISLATIVES/IN/Leg 2012 Candidatures T1 31 05 2012.xls", sheet=2, skip=2, header=FALSE) | |
mtch <- match(df$Nuance, str_trim(nuances[,1])) | |
df$Nuance.Lib <- str_trim(nuances[mtch, 2]) | |
# NOMBRE DE CANDIDATS | |
df$ncandidats <- sapply(1:nrow(res), function(x) length(na.omit(sapply(1:3, function(i) resL[[i]][x,1])))) | |
# EXPORT | |
write.table(df, file="OUT/Leg 2012 Résultats CirLG FE T2_gagnants.csv", sep=";", quote=F, row.names=F) | |
########################### | |
# PROFESSION DES GAGNANTS # | |
########################### | |
cddts <- read.xls("F:/METHODES/1205_LEGISLATIVES/IN/Leg 2012 Candidatures T1 31 05 2012.xls", sheet=1) | |
mtch <- match(df$Nom, cddts$Nom) | |
df$Profession <- cddts$Profession[mtch] | |
# AFFICHAGE | |
print(df[, c("Nom", "Prénom", "Profession", "Nuance.Lib")]) | |
# HISTOGRAMME | |
t <- table(df$Profession) | |
t <- t[order(t)] | |
bp <- barplot(t, horiz=T, yaxt="n") | |
axis(2, at=bp, labels = rownames(t), tick = FALSE, las = 2, cex.axis=.4) | |
# BAR PLOT | |
umpSoc <- df[df$Nuance.Lib %in% c("Socialiste", "Union pour un Mouvement Populaire"), ] | |
t <- table(umpSoc$Profession, umpSoc$Nuance.Lib) | |
tpc <- apply(t,1,function(x) x/sum(x)*100) | |
tpc <- t(tpc) | |
tpc <- tpc[order(tpc[,1]), ] | |
tpc <- tpc[complete.cases(tpc), ] | |
bp <- barplot(t(tpc), col=c("red", "blue"), horiz=T, yaxt="n") | |
axis(2, at=bp, labels = rownames(tpc), tick = FALSE, las = 2, cex.axis=.8) | |
################ | |
# AUTRES STATS # | |
################ | |
# PIE CHARTS | |
lCharts <- list(df$Sexe, df$Nuance.Lib) | |
lChartsT <- lapply(lCharts, function(x) table(x)) | |
par(mfrow=c(1,2)) | |
lapply(lChartsT, function(x) pie(x, labels=paste(names(x), " (", x, ")", sep=""), col=rainbow(length(x)), cex=.5)) | |
dev.off() | |
# DEPARTEMENTS AVEC LE PLUS D ABSTENTION | |
head(df[rev(order(df$X..Abs.Ins)), c(3, 4, 8)], 20) | |
# EXTREME DROITE | |
df[df$Nuance.Lib %in% c("Extrême droite", "Front National"), c("Libellé.du.département", "Code.de.la.circonscription" ,"Nom", "Prénom", "Nuance", "X..Voix.Exp", "ncandidats")] # DEPARTEMENTS ET GAGNANTS D EXTREME DROITE | |
# REPARTITION DES SEXES PAR PARTI | |
par(mar=c(5,15,0,5)) | |
t <- table(df$Sexe, df$Nuance.Lib) | |
bp <- barplot(t, col=c("lightblue", "pink"), horiz=T, yaxt="n") | |
axis(2, at=bp, labels = colnames(t), tick = FALSE, las = 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment