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

    url<-"https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv"
    
    data1<-read.csv(url)
  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(data1)
      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?

    5 columns, 51 rows

    str(murders)
    'data.frame':   51 obs. of  5 variables:
     $ state     : chr  "Alabama" "Alaska" "Arizona" "Arkansas" ...
     $ abb       : chr  "AL" "AK" "AZ" "AR" ...
     $ region    : Factor w/ 4 levels "Northeast","South",..: 2 4 4 2 4 4 1 2 2 2 ...
     $ population: num  4779736 710231 6392017 2915918 37253956 ...
     $ total     : num  135 19 232 93 1257 ...
    str(data1)
    'data.frame':   52 obs. of  4 variables:
     $ Rank      : int  1 2 3 4 5 6 7 8 9 10 ...
     $ State     : chr  "Alabama" "Alaska" "Arizona" "Arkansas" ...
     $ Postal    : chr  "AL" "AK" "AZ" "AR" ...
     $ Population: num  4849377 736732 6731484 2966369 38802500 ...

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(data1)
    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(murders)
    [1] "state"      "abb"        "region"     "population" "total"     
    names(data1)
    [1] "Rank"       "State"      "Postal"     "Population"
  3. Identify the variable that represents the state in each dataset.

    “state” for murders and “state’ for data1

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

    they are

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

    “state” and “population”

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

    because it helps you choose the correct columns to join and spot differences that could prevent matches. For example, the state columns are named state and State, so you have to take into account the capitalization difference.

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.

names(data1) <- tolower(names(data1))
names(murders)
[1] "state"      "abb"        "region"     "population" "total"     
names(data1)
[1] "rank"       "state"      "postal"     "population"
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.

    combined_data <- left_join(murders, data1, by = "state")
  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.

    head(combined_data)
           state abb region population.x total rank postal population.y
    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

    nrow(combined_data)
    [1] 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_data.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 datasets may have a different numbers of rows because the second dataset includes an additional location that is not in the murders dataset.

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

    Puerto Rico is the state that isnt on the second dataset

anti_join(data1,murders, by="state")
  rank       state postal population
1   40 Puerto Rico     PR    3548397