Activity

In-Class R Coding Activity

Step 1: Load the packages and data

  1. Load the tidyverse and dslabs packages.

    library(dplyr)
    Warning: package 'dplyr' was built under R version 4.5.3
    
    Attaching package: 'dplyr'
    The following objects are masked from 'package:stats':
    
        filter, lag
    The following objects are masked from 'package:base':
    
        intersect, setdiff, setequal, union
    library(dslabs)
    Warning: package 'dslabs' was built under R version 4.5.3
    library(tidyverse)
    Warning: package 'lubridate' was built under R version 4.5.3
    ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
    ✔ forcats   1.0.1     ✔ readr     2.1.6
    ✔ ggplot2   4.0.2     ✔ stringr   1.6.0
    ✔ lubridate 1.9.5     ✔ tibble    3.3.1
    ✔ purrr     1.2.1     ✔ tidyr     1.3.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
  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

    stateLevel <- 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(stateLevel)
    # 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?

    dim(murders)
    [1] 51  5
    dim(stateLevel)
    [1] 52  4

    Murders: 51 rows and 5 columns

    State Level: 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(stateLevel)
    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(stateLevel)
    [1] "Rank"       "State"      "Postal"     "Population"
  3. Identify the variable that represents the state in 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(stateLevel)
    # 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
  4. Are the state variable names identical in both datasets?

    No they’re not

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

    State and population but one dataset’s variable is capitalized.

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

    To see if a variable in the dataset match the other so it can be easily joined.

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

stateLevel <- stateLevel |>
  mutate(State = tolower(State))

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(stateLevel)
# 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

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?

    combined_data <- left_join(murders, stateLevel, by = c("state" = "State"))
    head(combined_data)
           state abb region population total Rank Postal Population
    1    alabama  AL  South    4779736   135    1     AL    4849377
    2     alaska  AK   West     710231    19    2     AK     736732
    3    arizona  AZ   West    6392017   232    3     AZ    6731484
    4   arkansas  AR  South    2915918    93    4     AR    2966369
    5 california  CA   West   37253956  1257    5     CA   38802500
    6   colorado  CO   West    5029196    65    6     CO    5355866
    dim(combined_data)
    [1] 51  8

    It has 51 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 numbers are different because stateLevel has an extra row which may be a duplicate row.

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

anti_join(murders, stateLevel, by = c("state" = "State"))
[1] state      abb        region     population total     
<0 rows> (or 0-length row.names)