1. Start a new Rmd document by going to File-->New File-->R Markdown-->OK. Save this document under the name yourname_hw7.Rmd. Delete the template Rmarkdown text and chunks (everything starting with ##R Markdown).

  2. Make a new code chunk (CTRL+Alt+I on Windows, Cmd + Option + I on a Mac). In that chuck, load tidyverse and magrittr libraries.

  3. Navigate to the Florida2016 folder where you saved the Florida 2016 Election Returns. Set your working directory to this directory.

  4. Modify the code from class to write a loop that

    1. Opens each file
    2. Keeps only the rows that contain county name, the number of votes for the two main parties’ (Dem and Rep) representatives in Congress, district number, candidate’s name, candidate’s party, and total votes.
    3. Summarizes to get total votes for each of the two major parties’ candidates for each district.
    4. combines the output into a single object called mydata.
  5. List the first ten rows of the resulting dataset, mydata, to check your work.

  6. Convert the following repeated code into a loop:

library(classdata)
data("terr_attacks.wide")
a<-mean(terr_attacks.wide[,5],na.rm=T)
b<-mean(terr_attacks.wide[,6],na.rm=T)
d<-mean(terr_attacks.wide[,7],na.rm=T)
e<-mean(terr_attacks.wide[,8],na.rm=T)
f<-mean(terr_attacks.wide[,9],na.rm=T)
g<-mean(terr_attacks.wide[,10],na.rm=T)
h<-mean(terr_attacks.wide[,11],na.rm=T)
i<-mean(terr_attacks.wide[,12],na.rm=T)
j<-mean(terr_attacks.wide[,13],na.rm=T)
k<-mean(terr_attacks.wide[,14],na.rm=T)
l<-mean(terr_attacks.wide[,15],na.rm=T)
m<-mean(terr_attacks.wide[,16],na.rm=T)

mymeans<-c(a,b,d,e,f,g,h,i,j,k,l,m)
  1. Now perform the same talk as that in Q6, only using summarise instead of a loop. Which way is easier/faster and more elegant? Why?

  2. Knit the RMarkdown file (press the “Knit” button above) and read through the corresponding html file.

For the submission: submit your solution in an R Markdown file and (just for insurance) submit the corresponding html file with it.