Class Activity 2

Author

Surafel Haile

Class Activity 2 :

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.5.3
Warning: package 'readr' was built under R version 4.5.3
Warning: package 'dplyr' was built under R version 4.5.3
── 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.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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)
Warning: package 'dslabs' was built under R version 4.5.3
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
url <- "https://raw.githubusercontent.com/plotly/datasets/master/2014_usa_states.csv"
USA_states<- read.csv(url)
head(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
glimpse(data)
function (..., list = character(), package = NULL, lib.loc = NULL, verbose = getOption("verbose"), 
    envir = .GlobalEnv, overwrite = TRUE)  
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…
  1. “murders” has 51 rows, and “2014_usa_states” has 52 rows. Using the head() function the we can the first 6 rows of each dataset.

    names(murders)
    [1] "state"      "abb"        "region"     "population" "total"     
    names(data)
    NULL
  1. The variable that represents states in both data sets is “state” .

  2. Yes , the name of the variable is identical in each dataset.

  3. The variable that are common in both data sets are : “state”and”population”.

  4. It’s important to inspect the data before joining them because we have to check for date types, correct columns needed to join them, and missing values.

    Prepare the state names:

murders <- murders |>
  mutate(state = tolower(state))
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
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
USA_states <- USA_states|> 
  mutate(State= tolower(State))
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
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
USA_states <- USA_states |> 
  rename(state = "State")
left_join(murders, USA_states, 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
combined_data <- left_join(murders, USA_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
glimpse(combined_data)
Rows: 51
Columns: 8
$ 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…
$ Rank       <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
$ Postal     <chr> "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL",…
$ Population <dbl> 4849377, 736732, 6731484, 2966369, 38802500, 5355866, 35966…
  1. 51 rows in the combined data.
write_csv(combined_data, "combined_murder_USAstates")
getwd()
[1] "C:/Users/suraf/OneDrive/Documents"
anti_join(murders, USA_states, by= "state")
[1] state      abb        region     population total     
<0 rows> (or 0-length row.names)