Overview

This quiz is meant to give you and me a sense of how well you understand the materials up to this point.

It will be worth the same number of points as a regular assignment.

Problem 1

Question 1a

First install a couple of packages. You don’t need to do this in this Rmarkdown document. Just be sure that “tidyverse” and “dslabs” are in your list of packages in the window on the bottom right.

If they aren’t use install.packages(““) directly in the Console in the window on the bottom left.

Question 1b

Load the packages you have just installed.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.2
## ✔ purrr     1.2.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)

Question 1c

View the different datasets available in dslabs.

data(package ="dslabs")

Question 1d

What year are the data in murders from?

?murders
## starting httpd help server ... done

The murders are from 2010.

Question 1e

Load the dataset “murders”.

data("murders")

Problem 2

Question 2a

How many columns (variables) are there in “murders” and what type or class is each variable? HINT: There is a single function that you can use to get this information and murders does not need to be in quotes.Put your code in the code chunk. If it runs correctly it will show all of the information I’m asking for and you don’t need to write it out separately.

str(murders)
## 'data.frame':    51 obs. of  5 variables:
##  $ state     : chr  "Alabama" "Alaska" "Arizona" "Arkansas" ...
##  $ abb       : chr  "AL" "AK" "AZ" "AR" ...
##  $ region    : Factor w/ 4 levels "Northeast","South",..: 2 4 4 2 4 4 1 2 2 2 ...
##  $ population: num  4779736 710231 6392017 2915918 37253956 ...
##  $ total     : num  135 19 232 93 1257 ...

Question 2b

List the state abbreviations in order from lowest number of murder to highest.

HINT: In the book, Introduction to Data Science, we were taught to first list the total murders in order and assign that a name. Then use the abb variable with this new name. Feel free to use the book.

ind <- order(murders$total)
murders$abb[ind]
##  [1] "VT" "ND" "NH" "WY" "HI" "SD" "ME" "ID" "MT" "RI" "AK" "IA" "UT" "WV" "NE"
## [16] "OR" "DE" "MN" "KS" "CO" "NM" "NV" "AR" "WA" "CT" "WI" "DC" "OK" "KY" "MA"
## [31] "MS" "AL" "IN" "SC" "TN" "AZ" "NJ" "VA" "NC" "MD" "OH" "MO" "LA" "IL" "GA"
## [46] "MI" "PA" "NY" "FL" "TX" "CA"

Question 2c

Create a new value that provides the crime rate for each state.Finish the r code in the chunk below.

murder_rate <- murders$total / murders$population * 100000
murder_rate
##  [1]  2.8244238  2.6751860  3.6295273  3.1893901  3.3741383  1.2924531
##  [7]  2.7139722  4.2319369 16.4527532  3.3980688  3.7903226  0.5145920
## [13]  0.7655102  2.8369608  2.1900730  0.6893484  2.2081106  2.6732010
## [19]  7.7425810  0.8280881  5.0748655  1.8021791  4.1786225  0.9992600
## [25]  4.0440846  5.3598917  1.2128379  1.7521372  3.1104763  0.3798036
## [31]  2.7980319  3.2537239  2.6679599  2.9993237  0.5947151  2.6871225
## [37]  2.9589340  0.9396843  3.5977513  1.5200933  4.4753235  0.9825837
## [43]  3.4509357  3.2013603  0.7959810  0.3196211  3.1246001  1.3829942
## [49]  1.4571013  1.7056487  0.8871131

Question 2d

Now make that value appear as a new variable in murders. HINT: You will be using mutate() check out the example in Basic Statistics using R p 28.

murders_plus <- mutate(murders, 
                       murder_rate = murders$total / murders$population *100000)

Question 2e

Show a summary of murders_plus.

summary(murders_plus)
##     state               abb                      region     population      
##  Length:51          Length:51          Northeast    : 9   Min.   :  563626  
##  Class :character   Class :character   South        :17   1st Qu.: 1696962  
##  Mode  :character   Mode  :character   North Central:12   Median : 4339367  
##                                        West         :13   Mean   : 6075769  
##                                                           3rd Qu.: 6636084  
##                                                           Max.   :37253956  
##      total         murder_rate     
##  Min.   :   2.0   Min.   : 0.3196  
##  1st Qu.:  24.5   1st Qu.: 1.2526  
##  Median :  97.0   Median : 2.6871  
##  Mean   : 184.4   Mean   : 2.7791  
##  3rd Qu.: 268.0   3rd Qu.: 3.3861  
##  Max.   :1257.0   Max.   :16.4528

Problem 3

Question 3a

Import the dataset 2018.UCR.PA.xlsx You can copy the code provided from the “import dataset” option in Environment.

library(readxl)
X2018_UCR_PA <- read_excel("C:/Users/Lexib/Downloads/2018.UCR.PA.xlsx")
View(X2018_UCR_PA)

Question 3b

Show the names of each variable. Check for strange spellings and correct them. Name 2018.UCR.PA to reflect this change.

HINT: We did this in week 3.

names(X2018_UCR_PA)
##  [1] "City"                                      
##  [2] "Population"                                
##  [3] "Violent\r\ncrime"                          
##  [4] "Murder and\r\nnonnegligent\r\nmanslaughter"
##  [5] "Rape"                                      
##  [6] "Robbery"                                   
##  [7] "Aggravated\r\nassault"                     
##  [8] "Property\r\ncrime"                         
##  [9] "Burglary"                                  
## [10] "Larceny-\r\ntheft"                         
## [11] "Motor\r\nvehicle\r\ntheft"                 
## [12] "Arson"
X2018_UCR_PA_cleaned <- X2018_UCR_PA %>%
  rename(violent.crime = 'Violent\r\ncrime') %>%
  rename(murder.manslaughter = 'Murder and\r\nnonnegligent\r\nmanslaughter') %>%
  rename(aggravated.assault = 'Aggravated\r\nassault') %>%
  rename(property.crime = 'Property\r\ncrime') %>%
  rename(larceny.theft = 'Larceny-\r\ntheft') %>%
  rename(motor.theft = 'Motor\r\nvehicle\r\ntheft')

summary(X2018_UCR_PA_cleaned)
##      City             Population      violent.crime      murder.manslaughter
##  Length:989         Min.   :    132   Min.   :    0.00   Min.   :  0.0000   
##  Class :character   1st Qu.:   2066   1st Qu.:    1.00   1st Qu.:  0.0000   
##  Mode  :character   Median :   4320   Median :    5.00   Median :  0.0000   
##                     Mean   :  10054   Mean   :   34.16   Mean   :  0.6977   
##                     3rd Qu.:   9088   3rd Qu.:   15.00   3rd Qu.:  0.0000   
##                     Max.   :1586916   Max.   :14420.00   Max.   :351.0000   
##       Rape             Robbery         aggravated.assault property.crime   
##  Min.   :   0.000   Min.   :   0.000   Min.   :   0.00    Min.   :    0.0  
##  1st Qu.:   0.000   1st Qu.:   0.000   1st Qu.:   1.00    1st Qu.:    9.0  
##  Median :   0.000   Median :   0.000   Median :   4.00    Median :   40.0  
##  Mean   :   2.971   Mean   :   9.449   Mean   :  21.05    Mean   :  164.6  
##  3rd Qu.:   1.000   3rd Qu.:   2.000   3rd Qu.:  11.00    3rd Qu.:  105.0  
##  Max.   :1095.000   Max.   :5262.000   Max.   :7712.00    Max.   :49145.0  
##     Burglary       larceny.theft      motor.theft          Arson        
##  Min.   :   0.00   Min.   :    0.0   Min.   :   0.00   Min.   :  0.000  
##  1st Qu.:   1.00   1st Qu.:    7.0   1st Qu.:   0.00   1st Qu.:  0.000  
##  Median :   5.00   Median :   32.0   Median :   1.00   Median :  0.000  
##  Mean   :  21.42   Mean   :  131.3   Mean   :  11.84   Mean   :  1.147  
##  3rd Qu.:  12.00   3rd Qu.:   89.0   3rd Qu.:   4.00   3rd Qu.:  0.000  
##  Max.   :6497.00   Max.   :36968.0   Max.   :5680.00   Max.   :430.000

Question 3c

Create a new variable called violent_crime_rate as you did in questions 2c and 2d.

violent_crime_rate = X2018_UCR_PA_cleaned$violent.crime / X2018_UCR_PA_cleaned$Population * 100000

X2018_UCR_PA_cleaned <- X2018_UCR_PA_cleaned %>%
  mutate(violent_crime_rate = X2018_UCR_PA_cleaned$violent.crime / X2018_UCR_PA_cleaned$Population * 100000)

violent_crime_rate
##   [1]   79.092592  161.550889   21.269054    0.000000  174.346202    0.000000
##   [7]  112.654900   72.167428    0.000000  558.909010  562.908870   86.494501
##  [13]  338.417815  376.368613  137.783221 1332.534811   30.590395  704.508857
##  [19]  260.416667  201.525838   52.493438  965.488907  149.253731  406.053894
##  [25]    0.000000   36.737693   35.910941  250.626566  255.955897  393.959291
##  [31]    0.000000    0.000000    0.000000  104.931794  203.665988   86.796692
##  [37]  103.519669    0.000000  248.946764  439.814815  417.188152    0.000000
##  [43]  184.569952   41.430742    0.000000   15.810277  258.302583   57.636888
##  [49]    0.000000    0.000000  115.534429  200.642055    0.000000  202.736949
##  [55]   71.174377    0.000000  368.893320   94.161959   33.932813    0.000000
##  [61]   65.082981  273.054651   75.053163    0.000000   58.060770    0.000000
##  [67]    0.000000  338.669951   66.622252   96.758587    0.000000  247.437257
##  [73]  135.317997  164.473684   49.285362  126.262626  710.900474    0.000000
##  [79] 1018.922853  232.853514    0.000000   64.655172  224.263135  268.997983
##  [85]  326.370757  119.880120  518.134715  180.788237  322.312331  176.961478
##  [91]  299.401198  236.259637  105.069609  178.731010  141.944642    4.923441
##  [97]  150.808884   31.595577  378.787879  304.878049  307.290466  151.130456
## [103]   81.732734    0.000000  339.065149  267.141585    0.000000  272.422464
## [109]  121.359223   75.528701   25.169897  147.475893  531.632111  166.944908
## [115]   93.095423    0.000000  139.452333  628.140704  383.141762   31.055901
## [121]   25.458248  213.106020   73.592543  151.080224  471.698113   15.831552
## [127]  332.196870  348.211459  150.274251   57.751540  286.232230  398.834177
## [133]   37.797657  186.661689  294.117647 1390.559451 1194.539249   25.015635
## [139]    0.000000  136.612022  349.172613   56.433409   48.309179  612.870276
## [145]    0.000000  551.497988   78.616352  279.069767  551.844322   91.743119
## [151]  116.395078    0.000000   48.053820  727.851700   33.396415  182.062093
## [157]  509.604077  323.624595   87.399854   83.518931  684.116597    0.000000
## [163]  491.065339  315.729047   61.873531   95.693780    0.000000  119.569550
## [169]   91.631032  453.720508   46.232085  665.504674  452.674897    0.000000
## [175]  312.082033  128.555359   25.678886  428.182172  126.903553   39.432177
## [181]    0.000000  192.431046   96.447516   84.486905  584.063413    0.000000
## [187]   75.602117  168.067227  302.964726 1392.263128  204.697264    0.000000
## [193]    0.000000    0.000000    0.000000  103.066220  159.165970  416.594341
## [199]   92.250923  196.335079  203.665988    0.000000   75.159714  113.450145
## [205]   86.058520  186.133085  202.456472    0.000000    0.000000  170.357751
## [211]  177.839635    0.000000  149.812734 1046.176046  185.299568   27.612868
## [217]  117.302053    0.000000   79.863092   75.693065  988.700565   14.753615
## [223]   68.965517  121.967746  260.642919  118.670886    0.000000    0.000000
## [229]  129.586134   52.530205  337.078652    0.000000  379.362671  305.289315
## [235]  254.068783   50.574713   44.189129   54.458816    0.000000  119.379228
## [241]  159.355081  189.155107    0.000000  384.193194   24.824162   64.662140
## [247]   21.727322  995.024876   66.181337  120.992136   67.773636  469.383401
## [253]    0.000000  555.555556  181.175050   68.770536  345.622120  134.318334
## [259]    0.000000   60.922541  162.074554   42.034468  164.803669   95.556617
## [265]  309.018376  149.342891   22.281640  115.141048  131.578947  215.053763
## [271]   92.464170   84.817642  389.863548    0.000000  222.158929  124.062149
## [277] 1032.036121  171.969046    0.000000   40.918623    0.000000  153.400375
## [283]  391.485197  483.749055    0.000000  169.491525   78.976465   51.463493
## [289]  122.189638   60.698027   49.019608  819.672131  148.273671    0.000000
## [295]  248.344371   22.483699  197.335964   20.399837  179.809915  508.228461
## [301]    0.000000    0.000000  268.817204   29.832936  145.306597    0.000000
## [307]   59.066745    0.000000  821.917808    0.000000    0.000000    0.000000
## [313]  392.824407  405.542413  207.612457  206.849000  153.566941    0.000000
## [319]    0.000000   22.261799    0.000000   25.119317  407.962299  120.821587
## [325]  548.245614   52.576236  112.937633    0.000000  293.652586   32.895819
## [331]   76.427558  313.039034  378.124135  164.149705  230.794593 1080.432173
## [337]    0.000000    0.000000  143.781452  185.701021   94.010207  104.522995
## [343]   48.561369    0.000000  474.895482  294.898260  164.068909  138.808560
## [349]  291.095890  133.809099  581.073603  122.335973   83.298626    0.000000
## [355]  675.390460   64.653779  312.337324    0.000000  790.638836  190.114068
## [361]   57.077626  332.778702  173.078436   26.300958    0.000000  145.666424
## [367]    0.000000    0.000000  215.146299  316.729053    0.000000   83.022001
## [373]  897.308076   41.695622    0.000000   92.707046  478.978180    0.000000
## [379]    0.000000   24.491795   21.593608  173.310225  109.565027  176.850296
## [385]    0.000000    0.000000   88.183422   89.385475  225.123818    0.000000
## [391]  487.092060  113.713896  411.879471  436.490615 1149.590458   57.323015
## [397]  278.357690  256.629598  161.498708   24.047132  729.546639    0.000000
## [403]  179.183546  130.472601   22.241993    0.000000    0.000000  553.250346
## [409]  215.285253  157.915515    0.000000    0.000000  701.126153    0.000000
## [415]  208.429829  860.215054  188.442211    0.000000   90.182168  244.131455
## [421]  423.504500   38.270188  303.951368  128.238010  184.501845  408.917029
## [427]  181.818182  163.666121  321.506043  199.800200   84.530854  315.457413
## [433]  170.551450  167.973124  171.772116  345.821326  439.667807   40.371417
## [439]    0.000000  262.295082  125.000000   41.788550    0.000000  100.806452
## [445]   84.674005    0.000000  178.213411  216.966804  643.316655  129.407959
## [451]   71.651569   80.681309   61.425061   25.989777  181.189260    0.000000
## [457]   30.517578   53.997503   30.293850  176.484907  321.569921  121.991793
## [463]   45.336788   92.387288  353.614145  179.694519   79.218379   71.275837
## [469]    0.000000    0.000000    0.000000  101.061142   31.387320  308.861962
## [475]   34.352456  360.317079    0.000000   56.980057  327.198364  123.777695
## [481]   29.682398   98.183603  113.798009   65.919578 1207.327227  342.596117
## [487]    0.000000   52.219321   29.708853   16.756032   61.425061    0.000000
## [493]    0.000000  242.057489    0.000000   92.336103   17.559263  145.208132
## [499] 1530.002391 1686.625063   97.529259  162.576450   88.681964  596.236259
## [505]  159.914712  485.436893    0.000000  611.620795   35.486160  133.351113
## [511]  450.692134   73.033086  280.561122    0.000000   85.421412  129.862464
## [517]  205.549846  603.968939  256.448654    0.000000  236.127509  119.331742
## [523]    0.000000  110.041265  967.883854  216.346154   65.274151  145.666424
## [529] 1029.230136  387.878788  228.029535   65.876153   26.616982   90.497738
## [535]   76.982294    0.000000   77.645780   42.812801  435.161010  116.863387
## [541]  186.150410  207.576544 1067.995728  831.393415    0.000000  146.412884
## [547]  312.049928   71.342163 1089.918256  397.846946   28.546960   84.495142
## [553]  104.384134  108.358371    0.000000  188.146754   20.243939  146.298644
## [559]   39.808917   70.101647    0.000000   54.212295  402.352213   87.145969
## [565]  189.214759    0.000000  116.414435  139.691409  431.474963   26.661927
## [571]  561.387494    0.000000   54.622423    0.000000   15.150367  127.110950
## [577]  318.091451  565.565161   55.699963   68.975031   44.424700   21.853147
## [583]   36.843269    0.000000  182.815356  402.536851   20.234723   30.542900
## [589]    0.000000  268.672757    0.000000   70.497004  139.806812   24.962556
## [595]   97.442144   58.932480    0.000000   59.853359  761.421320   55.418549
## [601]   16.926679   51.565589   40.174088  106.990014   49.319392  240.823783
## [607]   11.732958   51.361068   73.991861  183.635993  109.439124    0.000000
## [613]  288.612188  152.858453   19.971042    0.000000   67.842605    0.000000
## [619]  176.678445  139.189607  149.014214   43.642712  118.273211  184.256321
## [625]  114.140774   40.592653  198.965380    0.000000  101.488498    0.000000
## [631]    0.000000    0.000000  247.875354  245.793156   60.431387  251.455797
## [637]    0.000000    0.000000  180.598555  815.100815    0.000000   23.707918
## [643]   61.462815   30.975096  234.505863  212.765957  320.366133  410.762463
## [649]   45.400890   20.341741  155.464580  133.244504  139.386699  163.532298
## [655]  295.857988   54.210336  908.680737  188.102516    0.000000    0.000000
## [661] 1470.128245  578.758792  208.089478   88.521688  195.372751    0.000000
## [667]  238.322212   77.225498  725.513906  146.429376  194.324780  267.157992
## [673]  247.729149  128.534704   41.339396    0.000000    0.000000  193.798450
## [679]  189.907759  859.950860  325.227290  104.493208    0.000000  307.881773
## [685]  273.556231   86.745316  161.812298  430.643699  107.642626    0.000000
## [691]   97.072178  212.164074 1262.748907  701.964597   34.264177   23.668639
## [697]    0.000000  164.068909   40.160643   61.766523  150.375940   56.195561
## [703]  134.448760   58.626466   53.134963  198.722498   86.566207    0.000000
## [709]  160.170849    0.000000  187.265918    0.000000  121.097955  146.273678
## [715]  263.157895  632.729364  149.981252  118.017309    0.000000  128.700129
## [721]    0.000000   65.485740   72.575524    0.000000   62.800921    0.000000
## [727]  153.374233    0.000000   74.682599   47.709924   57.236889    0.000000
## [733]    0.000000  295.942721    0.000000    0.000000  249.609984  175.918686
## [739]   23.100023  457.721031  108.649725   19.936204   21.039344 1042.054814
## [745]  821.871855  213.219616  136.955033    0.000000    0.000000   71.329220
## [751]  811.965812  628.212450  494.146267  683.610868  538.277512  193.657710
## [757]  962.544465   82.724390  322.580645   75.628663  468.164794   17.908309
## [763]    0.000000   63.451777    0.000000    0.000000   16.594756   72.674419
## [769]  281.101920   46.274873   56.545095  194.678780    0.000000  320.586215
## [775]   34.944671  271.232412  240.690925  101.078167    0.000000  153.080750
## [781]    0.000000  119.303269 1226.993865    0.000000  106.851877  130.264872
## [787]   12.667849    0.000000   49.925112   27.107617    0.000000   30.196276
## [793]   11.866619   22.502250  200.697159  129.293384  295.130349 1515.151515
## [799]   65.267597  130.954330  150.195254  241.109102   62.893082  160.250438
## [805]  118.929633   53.588359  121.512835  388.719512   76.285963   37.950664
## [811]   34.024600  105.820106    0.000000  656.123822  105.871814  218.102508
## [817]    0.000000  436.963910  133.868809  189.182217   60.374321   25.113009
## [823]  520.833333   43.840421  170.415815    0.000000  950.469955  198.150594
## [829]  212.891780  182.561416   63.836578  354.289160    0.000000  241.019167
## [835]   60.618307    0.000000  179.157958  295.387412    0.000000  645.270844
## [841]  185.605279   69.444444  385.405961  197.732665   27.723870  100.755668
## [847]  511.197663  465.116279  132.425274   65.065336  495.049505    0.000000
## [853]  456.769984  541.418517   40.574810  298.507463   80.256822  435.019032
## [859]    0.000000  115.606936   48.804295   38.520801  345.489443   31.625553
## [865] 1394.728746  325.137167    0.000000 1815.384615   34.786066    0.000000
## [871]  252.911422  529.355729   56.398045   78.098851   28.005601   58.969218
## [877]   74.984514   90.976760   86.368216   25.793139  231.440271   38.175224
## [883]   44.998977   11.681561   39.898923   40.489928    0.000000   39.269586
## [889]   84.015963  254.161901  325.401668  424.328147   36.818851  208.159867
## [895]  951.733515    0.000000  100.200401    0.000000    0.000000   77.172403
## [901]  638.204225   73.224310   82.090573  191.152376   81.794015   76.511094
## [907]  126.707025  747.871159  395.604396   59.737157    0.000000  119.047619
## [913]  353.535354    0.000000 1923.864102    0.000000  185.873606  126.702566
## [919]  146.764510  103.626943  451.194013  330.033003  182.274989   93.720712
## [925]   70.671378   98.135427    8.352126   35.281665  124.146493   38.505968
## [931]  146.804836  470.219436  108.446801   59.808612  211.528292   55.928412
## [937]    0.000000    0.000000   73.448402  143.365369   35.306579  177.095632
## [943]  146.664644  402.576490  203.717851    0.000000   23.479690  146.878825
## [949]    0.000000  168.740772  128.700129  475.172250   80.677693  293.772033
## [955]  154.938670  124.019471   60.808756   17.946877   65.242212  112.317484
## [961]  305.810398   80.710250   65.679048   75.869793  182.481752   50.094623
## [967]   91.551138   31.046259    0.000000  515.843773  276.912426   16.170763
## [973]  335.289187  250.608874   27.314941  646.495992   69.492703    0.000000
## [979]    0.000000    0.000000  365.691489  124.354314  281.237445  747.046560
## [985] 1018.791035  204.022677  556.586271  109.920308   55.710306

Question 3d

Create a histogram based on violent_crime_rate. HINT: See page 31 in Basic Statistics using R

ggplot(X2018_UCR_PA_cleaned, aes(x=violent_crime_rate)) +
  geom_histogram(binwidth = 25)

Problem 3

Knit this Rmarkdown file and submit it via the link in the week 6 folder.