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

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

    download.file("https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv", "usa_states.csv")
    
    # Use built-in read.csv instead of read_csv
    usa_states <- read.csv("usa_states.csv")
    
    ds2<- usa_states
    view(ds2)
  4. Display the first six rows of each dataset

    head(ds1)
           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(ds2)
      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
  5. How many rows and columns does each dataset contain?

    ds1 : Rows: 51 Columns: 5

    ds2 : Rows: 52 Columns: 4

    Step 2: Explore the datasets

  1. Use glimpse() to examine the structure of both datasets.

    glimpse(ds1)
    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(ds2)
    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…
  2. Display the column names of each dataset using names().

    names(ds1)
    [1] "state"      "abb"        "region"     "population" "total"     
    names(ds2)
    [1] "Rank"       "State"      "Postal"     "Population"
  3. Identify the variable that represents the state in each dataset.

    In ds1 and ds2 both columns are called state

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

    No, one start with a capital letter and the other does not

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

    The variables that are common in both datasets are state and population

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

    It is important to inspect the datasets befor joining them because we need to join them through one variable, so we have to check if they are identical in both datasets.

    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.

ds2<- ds2 %>% rename(state = State)
ds2
   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
7     7          Connecticut     CT    3596677
8     8             Delaware     DE     935614
9     9 District of Columbia     DC     658893
10   10              Florida     FL   19893297
11   11              Georgia     GA   10097343
12   12               Hawaii     HI    1419561
13   13                Idaho     ID    1634464
14   14             Illinois     IL   12880580
15   15              Indiana     IN    6596855
16   16                 Iowa     IA    3107126
17   17               Kansas     KS    2904021
18   18             Kentucky     KY    4413457
19   19            Louisiana     LA    4649676
20   20                Maine     ME    1330089
21   21             Maryland     MD    5976407
22   22        Massachusetts     MA    6745408
23   23             Michigan     MI    9909877
24   24            Minnesota     MN    5457173
25   25          Mississippi     MS    2994079
26   26             Missouri     MO    6063589
27   27              Montana     MT    1023579
28   28             Nebraska     NE    1881503
29   29               Nevada     NV    2839098
30   30        New Hampshire     NH    1326813
31   31           New Jersey     NJ    8938175
32   32           New Mexico     NM    2085572
33   33             New York     NY   19746227
34   34       North Carolina     NC    9943964
35   35         North Dakota     ND     739482
36   36                 Ohio     OH   11594163
37   37             Oklahoma     OK    3878051
38   38               Oregon     OR    3970239
39   39         Pennsylvania     PA   12787209
40   40          Puerto Rico     PR    3548397
41   41         Rhode Island     RI    1055173
42   42       South Carolina     SC    4832482
43   43         South Dakota     SD     853175
44   44            Tennessee     TN    6549352
45   45                Texas     TX   26956958
46   46                 Utah     UT    2942902
47   47              Vermont     VT     626562
48   48             Virginia     VA    8326289
49   49           Washington     WA    7061530
50   50        West Virginia     WV    1850326
51   51            Wisconsin     WI    5757564
52   52              Wyoming     WY     584153
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.

    left_join(ds1,ds2)
    Joining with `by = join_by(state)`
                      state abb        region population total Rank Postal
    1               Alabama  AL         South    4779736   135    1     AL
    2                Alaska  AK          West     710231    19    2     AK
    3               Arizona  AZ          West    6392017   232    3     AZ
    4              Arkansas  AR         South    2915918    93    4     AR
    5            California  CA          West   37253956  1257    5     CA
    6              Colorado  CO          West    5029196    65    6     CO
    7           Connecticut  CT     Northeast    3574097    97    7     CT
    8              Delaware  DE         South     897934    38    8     DE
    9  District of Columbia  DC         South     601723    99    9     DC
    10              Florida  FL         South   19687653   669   10     FL
    11              Georgia  GA         South    9920000   376   11     GA
    12               Hawaii  HI          West    1360301     7   12     HI
    13                Idaho  ID          West    1567582    12   13     ID
    14             Illinois  IL North Central   12830632   364   14     IL
    15              Indiana  IN North Central    6483802   142   15     IN
    16                 Iowa  IA North Central    3046355    21   16     IA
    17               Kansas  KS North Central    2853118    63   17     KS
    18             Kentucky  KY         South    4339367   116   18     KY
    19            Louisiana  LA         South    4533372   351   19     LA
    20                Maine  ME     Northeast    1328361    11   20     ME
    21             Maryland  MD         South    5773552   293   21     MD
    22        Massachusetts  MA     Northeast    6547629   118   22     MA
    23             Michigan  MI North Central    9883640   413   23     MI
    24            Minnesota  MN North Central    5303925    53   24     MN
    25          Mississippi  MS         South    2967297   120   25     MS
    26             Missouri  MO North Central    5988927   321   26     MO
    27              Montana  MT          West     989415    12   27     MT
    28             Nebraska  NE North Central    1826341    32   28     NE
    29               Nevada  NV          West    2700551    84   29     NV
    30        New Hampshire  NH     Northeast    1316470     5   30     NH
    31           New Jersey  NJ     Northeast    8791894   246   31     NJ
    32           New Mexico  NM          West    2059179    67   32     NM
    33             New York  NY     Northeast   19378102   517   33     NY
    34       North Carolina  NC         South    9535483   286   34     NC
    35         North Dakota  ND North Central     672591     4   35     ND
    36                 Ohio  OH North Central   11536504   310   36     OH
    37             Oklahoma  OK         South    3751351   111   37     OK
    38               Oregon  OR          West    3831074    36   38     OR
    39         Pennsylvania  PA     Northeast   12702379   457   39     PA
    40         Rhode Island  RI     Northeast    1052567    16   41     RI
    41       South Carolina  SC         South    4625364   207   42     SC
    42         South Dakota  SD North Central     814180     8   43     SD
    43            Tennessee  TN         South    6346105   219   44     TN
    44                Texas  TX         South   25145561   805   45     TX
    45                 Utah  UT          West    2763885    22   46     UT
    46              Vermont  VT     Northeast     625741     2   47     VT
    47             Virginia  VA         South    8001024   250   48     VA
    48           Washington  WA          West    6724540    93   49     WA
    49        West Virginia  WV         South    1852994    27   50     WV
    50            Wisconsin  WI North Central    5686986    97   51     WI
    51              Wyoming  WY          West     563626     5   52     WY
       Population
    1     4849377
    2      736732
    3     6731484
    4     2966369
    5    38802500
    6     5355866
    7     3596677
    8      935614
    9      658893
    10   19893297
    11   10097343
    12    1419561
    13    1634464
    14   12880580
    15    6596855
    16    3107126
    17    2904021
    18    4413457
    19    4649676
    20    1330089
    21    5976407
    22    6745408
    23    9909877
    24    5457173
    25    2994079
    26    6063589
    27    1023579
    28    1881503
    29    2839098
    30    1326813
    31    8938175
    32    2085572
    33   19746227
    34    9943964
    35     739482
    36   11594163
    37    3878051
    38    3970239
    39   12787209
    40    1055173
    41    4832482
    42     853175
    43    6549352
    44   26956958
    45    2942902
    46     626562
    47    8326289
    48    7061530
    49    1850326
    50    5757564
    51     584153
  2. Use the state variable as the joining key.

    left_join(ds1,ds2, by = 'state')
                      state abb        region population total Rank Postal
    1               Alabama  AL         South    4779736   135    1     AL
    2                Alaska  AK          West     710231    19    2     AK
    3               Arizona  AZ          West    6392017   232    3     AZ
    4              Arkansas  AR         South    2915918    93    4     AR
    5            California  CA          West   37253956  1257    5     CA
    6              Colorado  CO          West    5029196    65    6     CO
    7           Connecticut  CT     Northeast    3574097    97    7     CT
    8              Delaware  DE         South     897934    38    8     DE
    9  District of Columbia  DC         South     601723    99    9     DC
    10              Florida  FL         South   19687653   669   10     FL
    11              Georgia  GA         South    9920000   376   11     GA
    12               Hawaii  HI          West    1360301     7   12     HI
    13                Idaho  ID          West    1567582    12   13     ID
    14             Illinois  IL North Central   12830632   364   14     IL
    15              Indiana  IN North Central    6483802   142   15     IN
    16                 Iowa  IA North Central    3046355    21   16     IA
    17               Kansas  KS North Central    2853118    63   17     KS
    18             Kentucky  KY         South    4339367   116   18     KY
    19            Louisiana  LA         South    4533372   351   19     LA
    20                Maine  ME     Northeast    1328361    11   20     ME
    21             Maryland  MD         South    5773552   293   21     MD
    22        Massachusetts  MA     Northeast    6547629   118   22     MA
    23             Michigan  MI North Central    9883640   413   23     MI
    24            Minnesota  MN North Central    5303925    53   24     MN
    25          Mississippi  MS         South    2967297   120   25     MS
    26             Missouri  MO North Central    5988927   321   26     MO
    27              Montana  MT          West     989415    12   27     MT
    28             Nebraska  NE North Central    1826341    32   28     NE
    29               Nevada  NV          West    2700551    84   29     NV
    30        New Hampshire  NH     Northeast    1316470     5   30     NH
    31           New Jersey  NJ     Northeast    8791894   246   31     NJ
    32           New Mexico  NM          West    2059179    67   32     NM
    33             New York  NY     Northeast   19378102   517   33     NY
    34       North Carolina  NC         South    9535483   286   34     NC
    35         North Dakota  ND North Central     672591     4   35     ND
    36                 Ohio  OH North Central   11536504   310   36     OH
    37             Oklahoma  OK         South    3751351   111   37     OK
    38               Oregon  OR          West    3831074    36   38     OR
    39         Pennsylvania  PA     Northeast   12702379   457   39     PA
    40         Rhode Island  RI     Northeast    1052567    16   41     RI
    41       South Carolina  SC         South    4625364   207   42     SC
    42         South Dakota  SD North Central     814180     8   43     SD
    43            Tennessee  TN         South    6346105   219   44     TN
    44                Texas  TX         South   25145561   805   45     TX
    45                 Utah  UT          West    2763885    22   46     UT
    46              Vermont  VT     Northeast     625741     2   47     VT
    47             Virginia  VA         South    8001024   250   48     VA
    48           Washington  WA          West    6724540    93   49     WA
    49        West Virginia  WV         South    1852994    27   50     WV
    50            Wisconsin  WI North Central    5686986    97   51     WI
    51              Wyoming  WY          West     563626     5   52     WY
       Population
    1     4849377
    2      736732
    3     6731484
    4     2966369
    5    38802500
    6     5355866
    7     3596677
    8      935614
    9      658893
    10   19893297
    11   10097343
    12    1419561
    13    1634464
    14   12880580
    15    6596855
    16    3107126
    17    2904021
    18    4413457
    19    4649676
    20    1330089
    21    5976407
    22    6745408
    23    9909877
    24    5457173
    25    2994079
    26    6063589
    27    1023579
    28    1881503
    29    2839098
    30    1326813
    31    8938175
    32    2085572
    33   19746227
    34    9943964
    35     739482
    36   11594163
    37    3878051
    38    3970239
    39   12787209
    40    1055173
    41    4832482
    42     853175
    43    6549352
    44   26956958
    45    2942902
    46     626562
    47    8326289
    48    7061530
    49    1850326
    50    5757564
    51     584153
  3. Store the joined dataset in a new object called combined_data.

    combined_data<- left_join(ds1,ds2, by = 'state')
  4. Display the first six rows of combined_data.

    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
  5. How many rows does the joined dataset contain?

    51

    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?

    Probably due to a missing state

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

    anti_join(ds1,ds2, by = 'state')
    [1] state      abb        region     population total     
    <0 rows> (or 0-length row.names)