Activity

In-Class R Coding Activity

Step 1: Load the packages and data

  1. Load the tidyverse and dslabs packages.

  2. Load the murders dataset from the dslabs package.

  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

  4. Display the first six rows of each dataset

  5. How many rows and columns does each dataset contain?

    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)
    data(murders)
    url <- "https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv"
    data <- read.csv(url)
    download.file(url, "usa_states_2014.csv")
murders1 <- slice(murders, 1:6)
usa_states <- slice(data, 1:6)
murders1
       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
usa_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

The murders dataset has 51 rows and 5 columns, 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.

  2. Display the column names of each dataset using names().

  3. Identify the variable that represents the state in each dataset.

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

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

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

    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(data)
    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…
names(murders)
[1] "state"      "abb"        "region"     "population" "total"     
names(data)
[1] "Rank"       "State"      "Postal"     "Population"

In the 2014 usa states, states are represented with “State” and in the murders dataset states are represented with “state.” The variable names are identical, but not exact. Variables that are common to both datasets are the “state” variables, “abbreviated” variables, and “population” variable. It’s important to inspect datasets before joining them because if they don’t have similar variables, joining them wouldn’t be possible.

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 <- ________________________
data <- data |> 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?

    combined_data <-left_join(murders, data, by="state")
    slice(combined_data, 1:6)
           state abb region population total Rank State Postal Population
    1    Alabama  AL  South    4779736   135   NA  <NA>   <NA>         NA
    2     Alaska  AK   West     710231    19   NA  <NA>   <NA>         NA
    3    Arizona  AZ   West    6392017   232   NA  <NA>   <NA>         NA
    4   Arkansas  AR  South    2915918    93   NA  <NA>   <NA>         NA
    5 California  CA   West   37253956  1257   NA  <NA>   <NA>         NA
    6   Colorado  CO   West    5029196    65   NA  <NA>   <NA>         NA
    View(combined_data)

    The joined dataset has 51 rows.

    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.