Please submit both the .Rmd and a .html file on Canvas.

You do not need any extra packages to do this homework (i.e., everything can be done using base R functions!)

## 2016 King County Elections Data

For much of this assignment you will be using 2016 general election data from King County, which I got directly from the county here. Take a look at the data using some of the functions we have learned about (colnames(), str(), head(), View(), etc.) to familiarize yourself with the dataset.

d <- read.csv("https://adamkucz.github.io/psych548/data/KCelections_2016.csv")

## Problem 1

Using a loop, determine the following information for each column in d. Hold your results in a dataframe called results where each row is one column in the dataframe:

• The column name (cname)
• The atomic type of the column vector (type)
• How many unique values are in each column (nvals)
• For character columns, what is the longest string? If there are ties for longest, paste them together separated by a semicolon and save that value (hint: the paste() function will help you with this; longstrings)
• For numeric columns, take the mean, standard deviation, and sum (saved in three separate columns; mean, sd, sum)

The final results dataframe should look like this:

cname type nvals longstrings mean sd sum
Precinct character 2517 ELECTIONS OFFICE NA NA NA
Race character 98 District Court Southwest Electoral District Judge Position 3 NA NA NA
LEG integer 18 NA 34.908377 12.580698 22443957
CC integer 10 NA 4.860023 2.589415 3124698
CG integer 4 NA 7.030109 2.413871 4521506
CounterGroup character 1 Total NA NA NA
Party character 17 Dem, Lib, Rep, CPN, DPN, GPN, LPN, RPN, SPN, SWN, DFW, NOP, PAR, Dcr NA NA NA
CounterType character 141 Gloria Estela La Riva & Eugene Puryear NA NA NA
SumOfCount integer 931 NA 198.484282 216.820801 127657746

## Problem 2

Using a loop, iterate over each of the 98 races (Race) that occurred in King County in 2016 and find the total number of votes cast in each race (SumOfCount). Make sure to exclude counts of blank votes (CounterType == "Times Blank Voted"), the number of registered voters (CounterType == "Registered Voters"), times votes were over counted (CounterType == "Times Over Voted"), and the sum of actual votes and blank votes (CounterType == "Times Counted"). Save this in a named vector of length 98 called totalcounts where the names are the name of the unique race.

##                                             Advisory Vote 14
##                                                       898839
##                                                       896411
##                                             Attorney General
##                                                       927792
##                                 Commissioner of Public Lands
##                                                       937491
##                                     Congressional District 1
##                                                       140849
##                            Court of Appeals Judge Position 1
##                                                       644189
##                                                     Governor
##                                                      1001594
##                                      Initiative Measure 1433
##                                                       985070
##                                      Initiative Measure 1464
##                                                       932435
##                                      Initiative Measure 1491
##                                                      1000687
##                                      Initiative Measure 1501
##                                                       983985
##                                       Initiative Measure 732
##                                                       951214
##                                       Initiative Measure 735
##                                                       946125
##                                       Insurance Commissioner
##                                                       923067
##                              King County Charter Amendment 1
##                                                       930332
##                              King County Charter Amendment 2
##                                                       957248
##            Legislative District 45 Representative Position 1
##                                                        69572
##            Legislative District 45 Representative Position 2
##                                                        55281
##                                          Lieutenant Governor
##                                                       958818
##                                           Secretary of State
##                                                       961177
##                                 Senate Joint Resolution 8210
##                                                       896267
##                                                State Auditor
##                                                       931073
##                       State Supreme Court Justice Position 1
##                                                       839874
##                       State Supreme Court Justice Position 5
##                                                       822670
##                       State Supreme Court Justice Position 6
##                                                       810806
##                                              State Treasurer
##                                                       824268
##                         Superintendent of Public Instruction
##                                                       817366
##                             Superior Court Judge Position 14
##                                                       786419
##                             Superior Court Judge Position 26
##                                                       774115
##                             Superior Court Judge Position 31
##                                                       781780
##                             Superior Court Judge Position 44
##                                                       779765
##                             Superior Court Judge Position 52
##                                                       776741
##                             Superior Court Judge Position 53
##                                                       781668
##                                US President & Vice President
##                                                      1028450
##                                                   US Senator
##                                                      1004490
##                                     Congressional District 8
##                                                       188933
##        King County Fire Protection District 27 Proposition 1
##                                                         2841
##             Legislative District 5 Representative Position 1
##                                                        72817
##             Legislative District 5 Representative Position 2
##                                                        73263
##                         Legislative District 5 State Senator
##                                                        74269
##      Proposed Eastside Regional Fire Authority Proposition 1
##                                                        16334
##                 Auburn School District No. 408 Proposition 1
##                                                        28951
##            Legislative District 30 Representative Position 1
##                                                        46786
##            Legislative District 30 Representative Position 2
##                                                        46834
##                                  Sound Transit Proposition 1
##                                                       856469
##                                     Congressional District 9
##                                                       271775
##            Legislative District 37 Representative Position 1
##                                                        63005
##            Legislative District 37 Representative Position 2
##                                                        62237
## District Court Southwest Electoral District Judge Position 3
##                                                        84136
##                     South King Fire and Rescue Proposition 1
##                                                        55075
##                   Kent School District No. 415 Proposition 1
##                                                        63721
##            Legislative District 11 Representative Position 1
##                                                        51389
##            Legislative District 11 Representative Position 2
##                                                        42843
##                        Legislative District 11 State Senator
##                                                        50937
##            Legislative District 31 Representative Position 1
##                                                        17832
##            Legislative District 31 Representative Position 2
##                                                        18729
##            Legislative District 47 Representative Position 1
##                                                        54950
##            Legislative District 47 Representative Position 2
##                                                        54983
##                                     Congressional District 7
##                                                       358543
##            Legislative District 34 Representative Position 1
##                                                        72998
##            Legislative District 34 Representative Position 2
##                                                        72784
##            Legislative District 41 Representative Position 1
##                                                        69984
##            Legislative District 41 Representative Position 2
##                                                        69933
##                        Legislative District 41 State Senator
##                                                        71643
##                               City of Bellevue Proposition 1
##                                                        57896
##                               City of Bellevue Proposition 2
##                                                        58025
##            Legislative District 48 Representative Position 1
##                                                        56425
##            Legislative District 48 Representative Position 2
##                                                        56085
##               Highline School District No. 401 Proposition 1
##                                                        49009
##                       City of Bothell Advisory Proposition 1
##                                                        12822
##                                City of Bothell Proposition 1
##                                                        12713
##             Legislative District 1 Representative Position 1
##                                                        20989
##             Legislative District 1 Representative Position 2
##                                                        20929
##                         Legislative District 1 State Senator
##                                                        21239
##            Legislative District 33 Representative Position 1
##                                                        47642
##            Legislative District 33 Representative Position 2
##                                                        47232
##                               City of Duvall Advisory Vote 1
##                                                         3718
##                                 City of Duvall Proposition 1
##                                                         3671
##                               City of Issaquah Proposition 1
##                                                        16813
##                                City of Kenmore Proposition 1
##                                                        11832
##            Legislative District 46 Representative Position 1
##                                                        75395
##            Legislative District 46 Representative Position 2
##                                                        64970
##                       City of Seattle Initiative Measure 124
##                                                       360379
##      District Court West Electoral District Judge Position 1
##                                                       238819
##      District Court West Electoral District Judge Position 4
##                                                       237947
##            Legislative District 32 Representative Position 1
##                                                        36336
##            Legislative District 32 Representative Position 2
##                                                        36388
##            Legislative District 36 Representative Position 1
##                                                        72054
##            Legislative District 36 Representative Position 2
##                                                        71501
##                        Legislative District 36 State Senator
##                                                        73533
##            Legislative District 43 Representative Position 1
##                                                        76241
##            Legislative District 43 Representative Position 2
##                                                        68913
##                              City of Shoreline Proposition 1
##                                                        28980
##            Legislative District 39 Representative Position 1
##                                                          248
##            Legislative District 39 Representative Position 2
##                                                          250
##                        Legislative District 39 State Senator
##                                                          196
##                             City of Snoqualmie Proposition 1
##                                                         6110
##                                City of Tukwila Proposition 1
##                                                         5788
• Which race (or races, in the event of a tie) got the most amount of votes?
## [1] "US President & Vice President"
• Which race for the least amount of votes?
## [1] "Legislative District 39 State Senator"

## Problem 3

Washington’s 45th legislative district runs from Kirkland on the west to Duvall on the east and Sammamish to the south. In 2016, Ramiro Valderrama was facing up against Roger Goodman for a spot in the WA House of Representatives. Let’s figure out which precincts Ramiro Valderrama won.

First, subset the data to include only the Legislative District 45 Representative Position 1 race. Call your dataframe d_ld45rp1.

Then, for each precinct that voted in that race, determine whether Ramiro Valderrama won (TRUE) or lost (FALSE) that precinct. Save these values in a named logical vector called valderrama_precincts where the names are the precinct names.

• How many precincts did Ramiro Valderrama win?
## [1] 9
• What percentage of all precincts is this? See if you can do this all with R code (i.e., do not write in the number of precincts yourself, let R figure that out)
## [1] 5.421687

## Problem 4

Create a new column in d called Party_Simplified. When Party is Dem or Dcr or DPN, Party_Simplified should take on the value Democrat. When Party is Rep or GP or RPN, Party_Simplified should take on the value Republican. When Party is NP (non-partisan), Party_Simplified should take on the value Non-partisan. For all other values of Party, Party_Simplified should take on the value Third Party.

Do this problem in two ways:

1. Using a loop
2. Not using a loop

Tip: The first method should take quite a while to run. To knit your document faster (the second time and on after initially knitting with this code), use the cache=TRUE chunk option on this chunk, which will store the output for all future knitting instead of re-running the code each time.

## Problem 5

Write a game where the computer is thinking of a random number between 1 and 10 (inclusive) and the user needs to guess which number the computer is thinking of. The game will take place in the console, once the R code for the game is executed. When the user guesses the wrong answer, the computer informs them of the error and asks them to guess again. When the user guesses the correct answer, the computer informs them of their success and the game is over. The computer should keep the same number “in mind” until the user guesses correctly. Below is some code to help you.

Bonus: Write the game such that the user can elect to play again (Y/N) without having to re-run the code. When the user plays again, the computer thinks of a new value for the user to guess. The game continues until the user says they want to quit (“N”) or rage quits with the Esc button.

# Note: keep eval=F for this chunk because it isn't friendly to the knitted format.
# I will run this code myself when you submit your assignment.

# This will sample 1 value between 1 and 10
number <- sample(1:10, 1)

# The readline() function prints to the console and waits for the user to type input. The user's
# input can be saved in an object for later use throughout your code.
guess <- readline("I am thinking of a number between 1 and 10. What number am I thinking of? ")
guess <- readline("That is not the number I am thinking of. Guess again: ")

# For bonus:
readline("That is the number I was thinking of! Would you like to play again? [Y/N] ")