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?

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?

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

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.


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)
library(readr)
library(dplyr)
data("murders")
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
view(murders)
url = "https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv"
states = read_csv(url)
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.
head(states)
# 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
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
glimpse(states)
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…
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…

The “states” dataset contains 52 rows and 4 columns.

The “murders” dataset contains 51 rows and 5 columns.

names(states)
[1] "Rank"       "State"      "Postal"     "Population"
names(murders)
[1] "state"      "abb"        "region"     "population" "total"     

The variable that represents state in each dataset is “state”. It is a character and categorical variable.

No. Both of the datasets use the word “state” as the variable name;however, the murders dataset has it lowercase while the states dataset has it capitalized.

Both of the datasets have similar variables such as: states, population and each respective state’s abbreviation. A key difference is although they both technically have the state’s abbreviation, murders has it named as “abb” while states has it named “Postal”. They are the same info but the column name are different. Another key difference is that the states dataset included Puerto Rico in as a state and murders doesn’t.

It is important to inspect data to look for key differences and similarities. We saw through Assignment 2 that if the variables differ from one another it causes issues when attempting to join the datasets.

murders = murders |>
  mutate(state = tolower(state))
states = states |> 
  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(states)
# 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
states = states |> 
  rename(state = State)
combined_data = left_join(murders, states, by = '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

The combined_data has 51 rows.

write_csv(combined_data, "combined_data.csv")
anti_join(states, murders, by = 'state')
# A tibble: 1 × 4
   Rank state       Postal Population
  <dbl> <chr>       <chr>       <dbl>
1    40 puerto rico PR        3548397