Activity

In-Class R Coding Activity - Rei Johnson & Maria Mendoza

Step 1: Load the packages and data

  1. Load the tidyverse and dslabs packages.

    library(tidyverse)
    ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
    ✔ dplyr     1.2.1     ✔ readr     2.2.0
    ✔ forcats   1.0.1     ✔ stringr   1.6.0
    ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
    ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
    ✔ purrr     1.2.2     
    ── 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)
  2. Load the murders dataset from the dslabs package.

    data(murders)
    murders
                      state abb        region population total
    1               Alabama  AL         South    4779736   135
    2                Alaska  AK          West     710231    19
    3               Arizona  AZ          West    6392017   232
    4              Arkansas  AR         South    2915918    93
    5            California  CA          West   37253956  1257
    6              Colorado  CO          West    5029196    65
    7           Connecticut  CT     Northeast    3574097    97
    8              Delaware  DE         South     897934    38
    9  District of Columbia  DC         South     601723    99
    10              Florida  FL         South   19687653   669
    11              Georgia  GA         South    9920000   376
    12               Hawaii  HI          West    1360301     7
    13                Idaho  ID          West    1567582    12
    14             Illinois  IL North Central   12830632   364
    15              Indiana  IN North Central    6483802   142
    16                 Iowa  IA North Central    3046355    21
    17               Kansas  KS North Central    2853118    63
    18             Kentucky  KY         South    4339367   116
    19            Louisiana  LA         South    4533372   351
    20                Maine  ME     Northeast    1328361    11
    21             Maryland  MD         South    5773552   293
    22        Massachusetts  MA     Northeast    6547629   118
    23             Michigan  MI North Central    9883640   413
    24            Minnesota  MN North Central    5303925    53
    25          Mississippi  MS         South    2967297   120
    26             Missouri  MO North Central    5988927   321
    27              Montana  MT          West     989415    12
    28             Nebraska  NE North Central    1826341    32
    29               Nevada  NV          West    2700551    84
    30        New Hampshire  NH     Northeast    1316470     5
    31           New Jersey  NJ     Northeast    8791894   246
    32           New Mexico  NM          West    2059179    67
    33             New York  NY     Northeast   19378102   517
    34       North Carolina  NC         South    9535483   286
    35         North Dakota  ND North Central     672591     4
    36                 Ohio  OH North Central   11536504   310
    37             Oklahoma  OK         South    3751351   111
    38               Oregon  OR          West    3831074    36
    39         Pennsylvania  PA     Northeast   12702379   457
    40         Rhode Island  RI     Northeast    1052567    16
    41       South Carolina  SC         South    4625364   207
    42         South Dakota  SD North Central     814180     8
    43            Tennessee  TN         South    6346105   219
    44                Texas  TX         South   25145561   805
    45                 Utah  UT          West    2763885    22
    46              Vermont  VT     Northeast     625741     2
    47             Virginia  VA         South    8001024   250
    48           Washington  WA          West    6724540    93
    49        West Virginia  WV         South    1852994    27
    50            Wisconsin  WI North Central    5686986    97
    51              Wyoming  WY          West     563626     5
  3. Import the state-level CSV dataset directly from the following URL using read_csv():

    https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv

    us_states = read.csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv")
    us_states
       Rank                State Postal Population
    1     1              Alabama     AL    4849377
    2     2               Alaska     AK     736732
    3     3              Arizona     AZ    6731484
    4     4             Arkansas     AR    2966369
    5     5           California     CA   38802500
    6     6             Colorado     CO    5355866
    7     7          Connecticut     CT    3596677
    8     8             Delaware     DE     935614
    9     9 District of Columbia     DC     658893
    10   10              Florida     FL   19893297
    11   11              Georgia     GA   10097343
    12   12               Hawaii     HI    1419561
    13   13                Idaho     ID    1634464
    14   14             Illinois     IL   12880580
    15   15              Indiana     IN    6596855
    16   16                 Iowa     IA    3107126
    17   17               Kansas     KS    2904021
    18   18             Kentucky     KY    4413457
    19   19            Louisiana     LA    4649676
    20   20                Maine     ME    1330089
    21   21             Maryland     MD    5976407
    22   22        Massachusetts     MA    6745408
    23   23             Michigan     MI    9909877
    24   24            Minnesota     MN    5457173
    25   25          Mississippi     MS    2994079
    26   26             Missouri     MO    6063589
    27   27              Montana     MT    1023579
    28   28             Nebraska     NE    1881503
    29   29               Nevada     NV    2839098
    30   30        New Hampshire     NH    1326813
    31   31           New Jersey     NJ    8938175
    32   32           New Mexico     NM    2085572
    33   33             New York     NY   19746227
    34   34       North Carolina     NC    9943964
    35   35         North Dakota     ND     739482
    36   36                 Ohio     OH   11594163
    37   37             Oklahoma     OK    3878051
    38   38               Oregon     OR    3970239
    39   39         Pennsylvania     PA   12787209
    40   40          Puerto Rico     PR    3548397
    41   41         Rhode Island     RI    1055173
    42   42       South Carolina     SC    4832482
    43   43         South Dakota     SD     853175
    44   44            Tennessee     TN    6549352
    45   45                Texas     TX   26956958
    46   46                 Utah     UT    2942902
    47   47              Vermont     VT     626562
    48   48             Virginia     VA    8326289
    49   49           Washington     WA    7061530
    50   50        West Virginia     WV    1850326
    51   51            Wisconsin     WI    5757564
    52   52              Wyoming     WY     584153
  4. Display the first six rows of each dataset

    head(murders)
           state abb region population total
    1    Alabama  AL  South    4779736   135
    2     Alaska  AK   West     710231    19
    3    Arizona  AZ   West    6392017   232
    4   Arkansas  AR  South    2915918    93
    5 California  CA   West   37253956  1257
    6   Colorado  CO   West    5029196    65
    head(us_states)
      Rank      State Postal Population
    1    1    Alabama     AL    4849377
    2    2     Alaska     AK     736732
    3    3    Arizona     AZ    6731484
    4    4   Arkansas     AR    2966369
    5    5 California     CA   38802500
    6    6   Colorado     CO    5355866
  5. How many rows and columns does each dataset contain?

    The murders dataset has 5 columns across 51 rows and the 2014_usa_states dataset has 52 rows and 4 columns.

Step 2: Explore the datasets

  1. Use glimpse() to examine the structure of both datasets.

    glimpse(murders)
    Rows: 51
    Columns: 5
    $ state      <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "California", "…
    $ abb        <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL",…
    $ region     <fct> South, West, West, South, West, West, Northeast, South, Sou…
    $ population <dbl> 4779736, 710231, 6392017, 2915918, 37253956, 5029196, 35740…
    $ total      <dbl> 135, 19, 232, 93, 1257, 65, 97, 38, 99, 669, 376, 7, 12, 36…
    glimpse(us_states)
    Rows: 52
    Columns: 4
    $ Rank       <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
    $ State      <chr> "Alabama", "Alaska", "Arizona", "Arkansas", "California", "…
    $ Postal     <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL",…
    $ Population <dbl> 4849377, 736732, 6731484, 2966369, 38802500, 5355866, 35966…
  2. Display the column names of each dataset using names().

    names(murders)
    [1] "state"      "abb"        "region"     "population" "total"     
    names(us_states)
    [1] "Rank"       "State"      "Postal"     "Population"
  3. Identify the variable that represents the state in each dataset.

    state and State represent the state in each dataset.

  4. Are the state variable names identical in both datasets?

    No, one is capitalized.

  5. Identify the variables that are common to both datasets.

    State and population.

  6. Why is it important to inspect the datasets before joining them?

    different columns will have different names but they will share the same data.

3. Prepare the state names

The murders dataset has 51 observations, including Washington, D.C. The second dataset contains 52 rows, so not every row necessarily represents a state.

murders <- murders |> 
  mutate(state = tolower(state))

us_states <- us_states |> 
  rename("state" = State) |> 
  mutate(state = tolower(state))

Step 4: Join the datasets

  1. Use left_join() to join the murders dataset with the state-level dataset.

    combined_data = left_join(murders, us_states, by="state")
    combined_data <- combined_data[][0:5]
    combined_data
                      state abb        region population total
    1               alabama  AL         South    4779736   135
    2                alaska  AK          West     710231    19
    3               arizona  AZ          West    6392017   232
    4              arkansas  AR         South    2915918    93
    5            california  CA          West   37253956  1257
    6              colorado  CO          West    5029196    65
    7           connecticut  CT     Northeast    3574097    97
    8              delaware  DE         South     897934    38
    9  district of columbia  DC         South     601723    99
    10              florida  FL         South   19687653   669
    11              georgia  GA         South    9920000   376
    12               hawaii  HI          West    1360301     7
    13                idaho  ID          West    1567582    12
    14             illinois  IL North Central   12830632   364
    15              indiana  IN North Central    6483802   142
    16                 iowa  IA North Central    3046355    21
    17               kansas  KS North Central    2853118    63
    18             kentucky  KY         South    4339367   116
    19            louisiana  LA         South    4533372   351
    20                maine  ME     Northeast    1328361    11
    21             maryland  MD         South    5773552   293
    22        massachusetts  MA     Northeast    6547629   118
    23             michigan  MI North Central    9883640   413
    24            minnesota  MN North Central    5303925    53
    25          mississippi  MS         South    2967297   120
    26             missouri  MO North Central    5988927   321
    27              montana  MT          West     989415    12
    28             nebraska  NE North Central    1826341    32
    29               nevada  NV          West    2700551    84
    30        new hampshire  NH     Northeast    1316470     5
    31           new jersey  NJ     Northeast    8791894   246
    32           new mexico  NM          West    2059179    67
    33             new york  NY     Northeast   19378102   517
    34       north carolina  NC         South    9535483   286
    35         north dakota  ND North Central     672591     4
    36                 ohio  OH North Central   11536504   310
    37             oklahoma  OK         South    3751351   111
    38               oregon  OR          West    3831074    36
    39         pennsylvania  PA     Northeast   12702379   457
    40         rhode island  RI     Northeast    1052567    16
    41       south carolina  SC         South    4625364   207
    42         south dakota  SD North Central     814180     8
    43            tennessee  TN         South    6346105   219
    44                texas  TX         South   25145561   805
    45                 utah  UT          West    2763885    22
    46              vermont  VT     Northeast     625741     2
    47             virginia  VA         South    8001024   250
    48           washington  WA          West    6724540    93
    49        west virginia  WV         South    1852994    27
    50            wisconsin  WI North Central    5686986    97
    51              wyoming  WY          West     563626     5
  2. Use the state variable as the joining key. Done

  3. Store the joined dataset in a new object called combined_data. Done

  4. Display the first six rows of combined_data.

    head(combined_data)
           state abb region population total
    1    alabama  AL  South    4779736   135
    2     alaska  AK   West     710231    19
    3    arizona  AZ   West    6392017   232
    4   arkansas  AR  South    2915918    93
    5 california  CA   West   37253956  1257
    6   colorado  CO   West    5029196    65
  5. How many rows does the joined dataset contain?

    51 total rows.

    Export the final dataset

  6. Use write_csv() to export combined_data as a CSV file named combined_murders.csv

    write_csv(combined_data, "combined_murders.csv")

Step 5: Investigate unmatched states

  1. The murders dataset contains 51 observations, while the second dataset contains 52 rows. Why might the number of rows differ?

    The rows differ because one might have a total population row and one may not have a totals row.

  2. Use anti_join() to identify the states in murders that do not have a matching state in the second dataset.

    anti_join(us_states, murders)
    Joining with `by = join_by(state)`
      Rank       state Postal Population
    1   40 puerto rico     PR    3548397