Activity

Author

G Oliveira

In-Class R Coding Activity

Step 1: Load the packages and data

  • 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)
  • Load the murders dataset from the dslabs package.
data("murders")
  • 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

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.
  • Display the first six rows of each dataset
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
  • How many rows and columns does each dataset contain?

murders has 51 rows and 5 columns.

states has 52 rows and 4 columns.

Step 2: Explore the datasets

  • 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(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…
  • Display the column names of each dataset using names().
names(murders)
[1] "state"      "abb"        "region"     "population" "total"     
names(states)
[1] "Rank"       "State"      "Postal"     "Population"
  • Identify the variable that represents the state in each dataset.

For both dataset “murders” and “state” the variable that represents states are “State”

  • Are the state variable names identical in both datasets?

No, they have a difference in the capital letter

  • Identify the variables that are common to both datasets.

state, population and abb/postal

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

We can determinate if we can join those two datasets, if there is something in common or not.

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 <- ________________________
scores <- states |>
  mutate(state = tolower(State))

Step 4: Join the datasets

  • Use left_join() to join the murders dataset with the state-level dataset.
combined_data <- left_join(murders, scores, by = "state") 
  • Use the state variable as the joining key.

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

  • Display the first six rows of combined_data.

head(combined_data)
       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
  • How many rows does the joined dataset contain?

    51 rows

    Export the final dataset

  • 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

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

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

matching <- anti_join(murders, scores, by = "state")
matching
                  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