--- title: "R course - Exercice 1" author: "First Name - Last Name" date: "Master 2 D3S" output: pdf_document: default html_document: df_print: paged --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## Summary We are interested in the results of the first round of the French presidential election that took place in 2017. We will work with the raw data produced by the French Ministry of the Interior. The objective is to see what is the influence of a voting system on the results of an election. ## Notation You will have to return the exercice in *.pdf* or *.html* format, which would have been done with **R** Markdown if possible. It should contain the lines of code used to answer the questions, but you should also explain what you are doing. **Remark:** try to use the least amount of command lines per question asked. For the first 5 questions, it is *a priori* possible to deal with only one command line per question. If you use several lines, it does not really matter, but the idea of this exercice is also to make you look for and find the most "elegant" and simple solutions to answer a given question. ## The election The raw data are given at: https://www.data.gouv.fr/s/resources/election-presidentielle-des-23-avril-et-7-mai-2017-resultats-definitifs-du-1er-tour-par-bureaux-de-vote/20170427-100955/PR17_BVot_T1_FE.txt First and last names of the 11 candidates at the presidential election in 2007 are ranked according to their appearance at the electoral boards : * Nicolas DUPONT-AIGNAN * Marine LE PEN * Emmanuel MACRON * Benoît HAMON * Nathalie ARTHAUD * Philippe POUTOU * Jacques CHEMINADE * Jean LASSALLE * Jean-Luc MÉLENCHON * François ASSELINEAU * François FILLON In the file, a row crresponds to the results of the election in one vote place. For each of the 69242 vote places, we observe 98 variables: * Code departement * Name departement * Code circonscription * Name circonscription * Code commune * Name commune * Code vote place * Number of people registered (i.e. number of people who can vote in the vote place) * Number of abstention (i.e. number of people who were registered but who did not vote) * % Abs/Reg (ratio "abstention" on "registered") * Voters (i.e. number of people who did vote) * % Vot/Ins (ratio "Voters" on "registered") * Number of white vote (a white vote is a voter who did not select anyone among the candidates) * % white/Reg (ratio "white" on "registered") * % white/Vot (ratio "white" on "Voters") * Number of null votes (a null vote is a voter who did not respect the procedure of voting) * % null/Reg (ratio "nuls" on "registered") * % null/Vot (ratio "nuls" on "Voters") * Number of valid votes (i.e. number of voters minus number of null votes minus number of white votes) * % valid/Reg (ratio "valid" on "registered") * % valid/Vot (ratio "valid" on "Voters") Then, we have for each of the 11 candidates: * number of bilboard * Sexe of the candidate $i$, $i=1,...,11$ * Last name of the candidate $i$ * First name of the candidate $i$ * Number of votes obtained by candidate $i$ * % (votes for $i$)/Reg (ratio "(votes for $i$)" on "registered") * % (votes for $i$)/valid (ratio "(votes for $i$)" on "Voters") ## Q1. Import the data Import the data by using one of the method seen in the course. **Remark:** the 1st line of the file is supposed to correspond to the names of the columns, but only the first 28 columns are filled in the header. To import the data, this information will have to be taken into account. ```{r} ## Add your anwser here ``` ## Q2. Select variables * Keep only the following variables: 2, 4, 6, 7, 8, 26, 33, 40, 47, 54, 61, 68, 75, 82, 89, 96 ```{r} ## Add your anwser here ``` * Give the following names: * "departement", * "circonscription", * "commune", * "bureau_vote", * "inscrits", * "dupont_aignan", * "le_pen", * "macron", * "hamon", * "arthaud", * "poutou", * "cheminade", * "lassale", * "melenchon", * "asselineau", * "fillon" ```{r} ## Add your anwser here ``` ## Q3. Messy to tidy data Transform the data so that one raw corresponds to the result of a candidate in a vote place. The file will have the following form: ```{r} ## Add your anwser here ``` ```{r, eval = F} head(tidy_votes) ``` ## Q4. Analysis of a first voting method Among the 11 candidates, calculate the percentage of votes obtained by each candidate across the country. Who has the most votes? ```{r} ## Add your anwser here ``` Compare your results with: https://www.interieur.gouv.fr/Archives/Archives-elections/Election-presidentielle-2017/Election-presidentielle-2017-resultats-globaux-du-premier-tour ## Q5. Analysis of a 2nd voting method * Create a table where a row corresponds to a department and gives the number of registered as well as the name of the candidate who won the most votes within the department. ```{r} ## Add your anwser here ``` * We will assume that we have 578 representants in France who are allocated to the departments of France. Propose a method of re-allocation of these 578 representants in these departments. ```{r} ## Add your anwser here ``` * who is the candidate who has won the biggest number of departements? ```{r} ## Add your anwser here ``` * Assuming that the candidate who has won in a department wins all the representants of the department, which candidate won the largest number of representants? ```{r} ## Add your anwser here ``` ## Q6 Ranking Finally, we will assign the following notes, by vote place: * the score of 10 to the candidate who came first, * the score of 5 to the candidate who is second, * the score of 2 to the candidate arriving third. Who is the candidate with the highest score on all vote places? ```{r} ## Add your anwser here ```