Activity

In-Class R Coding Activity

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(dplyr)
    library(dslabs)
  2. Load the murders dataset from the dslabs package.

    data(murders)
  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

    scores <- read_csv("https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv")
    Rows: 52 Columns: 4
    ── Column specification ────────────────────────────────────────────────────────
    Delimiter: ","
    chr (2): State, Postal
    dbl (2): Rank, Population
    
    ℹ Use `spec()` to retrieve the full column specification for this data.
    ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
  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(scores)
    # A tibble: 6 × 4
       Rank State      Postal Population
      <dbl> <chr>      <chr>       <dbl>
    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?

    structure(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
    structure(scores)
    # A tibble: 52 × 4
        Rank State                Postal Population
       <dbl> <chr>                <chr>       <dbl>
     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
    # ℹ 42 more rows

Murders has 51 rows and 5 columns, scores has 52 rows and 10 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(scores)
    Rows: 52
    Columns: 4
    $ Rank       <dbl> 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(scores)
    [1] "Rank"       "State"      "Postal"     "Population"
  3. Identify the variable that represents the state in each dataset.

    In murders, the variable is state, and in scores it is State.

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

    No they are not identical, one is capital one is not.

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

    The common variables are state, but one is capitalized.

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

    It is important to check the column names, data types, number of rows, and values before joining them together.

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))

scores <- ________________________
murders <- murders |>
  mutate(state = tolower(state))
scores <- scores |>
  mutate(state = tolower(State))

Step 4: Join the datasets

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

  2. Use the state variable as the joining key.

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

  4. Display the first six rows of combined_data.

  5. How many rows does the joined dataset contain?

    Export the final dataset

  6. Use write_csv() to export combined_data as a CSV file named 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?

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