Mon 9/14 & Wed 9/16, DATA 110 Haleluya Tesfamariam

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(dplyr)
library(ggplot2)
data(heights)

# Logical filtering criteria
tall_male <- heights$sex == "Male" & heights$height > 70
short_female <- heights$sex == "Female" & heights$height < 55

# Descriptive statistics
height_grp <- heights %>% group_by(sex)
height_grp %>% summarize(avg = mean(height), stdev = sd(height))
# A tibble: 2 × 3
  sex      avg stdev
  <fct>  <dbl> <dbl>
1 Female  64.9  3.76
2 Male    69.3  3.61
table(heights$sex)

Female   Male 
   238    812 
# Calculate proportions
f <- heights %>% count(sex) %>% mutate(proportion = n / sum(n))

# Boxplot of heights by sex
heights %>% ggplot(aes(x = sex, y = height, fill = sex)) + 
  geom_boxplot() + 
  scale_fill_manual(values = c("white", "red"))

# View sorted datasets
murders %>% arrange(population)
                  state abb        region population total
1               Wyoming  WY          West     563626     5
2  District of Columbia  DC         South     601723    99
3               Vermont  VT     Northeast     625741     2
4          North Dakota  ND North Central     672591     4
5                Alaska  AK          West     710231    19
6          South Dakota  SD North Central     814180     8
7              Delaware  DE         South     897934    38
8               Montana  MT          West     989415    12
9          Rhode Island  RI     Northeast    1052567    16
10        New Hampshire  NH     Northeast    1316470     5
11                Maine  ME     Northeast    1328361    11
12               Hawaii  HI          West    1360301     7
13                Idaho  ID          West    1567582    12
14             Nebraska  NE North Central    1826341    32
15        West Virginia  WV         South    1852994    27
16           New Mexico  NM          West    2059179    67
17               Nevada  NV          West    2700551    84
18                 Utah  UT          West    2763885    22
19               Kansas  KS North Central    2853118    63
20             Arkansas  AR         South    2915918    93
21          Mississippi  MS         South    2967297   120
22                 Iowa  IA North Central    3046355    21
23          Connecticut  CT     Northeast    3574097    97
24             Oklahoma  OK         South    3751351   111
25               Oregon  OR          West    3831074    36
26             Kentucky  KY         South    4339367   116
27            Louisiana  LA         South    4533372   351
28       South Carolina  SC         South    4625364   207
29              Alabama  AL         South    4779736   135
30             Colorado  CO          West    5029196    65
31            Minnesota  MN North Central    5303925    53
32            Wisconsin  WI North Central    5686986    97
33             Maryland  MD         South    5773552   293
34             Missouri  MO North Central    5988927   321
35            Tennessee  TN         South    6346105   219
36              Arizona  AZ          West    6392017   232
37              Indiana  IN North Central    6483802   142
38        Massachusetts  MA     Northeast    6547629   118
39           Washington  WA          West    6724540    93
40             Virginia  VA         South    8001024   250
41           New Jersey  NJ     Northeast    8791894   246
42       North Carolina  NC         South    9535483   286
43             Michigan  MI North Central    9883640   413
44              Georgia  GA         South    9920000   376
45                 Ohio  OH North Central   11536504   310
46         Pennsylvania  PA     Northeast   12702379   457
47             Illinois  IL North Central   12830632   364
48             New York  NY     Northeast   19378102   517
49              Florida  FL         South   19687653   669
50                Texas  TX         South   25145561   805
51           California  CA          West   37253956  1257
murders %>% arrange(desc(total))
                  state abb        region population total
1            California  CA          West   37253956  1257
2                 Texas  TX         South   25145561   805
3               Florida  FL         South   19687653   669
4              New York  NY     Northeast   19378102   517
5          Pennsylvania  PA     Northeast   12702379   457
6              Michigan  MI North Central    9883640   413
7               Georgia  GA         South    9920000   376
8              Illinois  IL North Central   12830632   364
9             Louisiana  LA         South    4533372   351
10             Missouri  MO North Central    5988927   321
11                 Ohio  OH North Central   11536504   310
12             Maryland  MD         South    5773552   293
13       North Carolina  NC         South    9535483   286
14             Virginia  VA         South    8001024   250
15           New Jersey  NJ     Northeast    8791894   246
16              Arizona  AZ          West    6392017   232
17            Tennessee  TN         South    6346105   219
18       South Carolina  SC         South    4625364   207
19              Indiana  IN North Central    6483802   142
20              Alabama  AL         South    4779736   135
21          Mississippi  MS         South    2967297   120
22        Massachusetts  MA     Northeast    6547629   118
23             Kentucky  KY         South    4339367   116
24             Oklahoma  OK         South    3751351   111
25 District of Columbia  DC         South     601723    99
26          Connecticut  CT     Northeast    3574097    97
27            Wisconsin  WI North Central    5686986    97
28             Arkansas  AR         South    2915918    93
29           Washington  WA          West    6724540    93
30               Nevada  NV          West    2700551    84
31           New Mexico  NM          West    2059179    67
32             Colorado  CO          West    5029196    65
33               Kansas  KS North Central    2853118    63
34            Minnesota  MN North Central    5303925    53
35             Delaware  DE         South     897934    38
36               Oregon  OR          West    3831074    36
37             Nebraska  NE North Central    1826341    32
38        West Virginia  WV         South    1852994    27
39                 Utah  UT          West    2763885    22
40                 Iowa  IA North Central    3046355    21
41               Alaska  AK          West     710231    19
42         Rhode Island  RI     Northeast    1052567    16
43                Idaho  ID          West    1567582    12
44              Montana  MT          West     989415    12
45                Maine  ME     Northeast    1328361    11
46         South Dakota  SD North Central     814180     8
47               Hawaii  HI          West    1360301     7
48        New Hampshire  NH     Northeast    1316470     5
49              Wyoming  WY          West     563626     5
50         North Dakota  ND North Central     672591     4
51              Vermont  VT     Northeast     625741     2
table(murders$region)

    Northeast         South North Central          West 
            9            17            12            13 
# Custom regional filtering (Note: `%notin%` requires an external definition or package)
murders_not_sw <- filter(murders, region %notin% c("West", "South"))
murders_ne <- filter(murders, region %in% c("Northeast"))
murders_not_sw
           state abb        region population total
1    Connecticut  CT     Northeast    3574097    97
2       Illinois  IL North Central   12830632   364
3        Indiana  IN North Central    6483802   142
4           Iowa  IA North Central    3046355    21
5         Kansas  KS North Central    2853118    63
6          Maine  ME     Northeast    1328361    11
7  Massachusetts  MA     Northeast    6547629   118
8       Michigan  MI North Central    9883640   413
9      Minnesota  MN North Central    5303925    53
10      Missouri  MO North Central    5988927   321
11      Nebraska  NE North Central    1826341    32
12 New Hampshire  NH     Northeast    1316470     5
13    New Jersey  NJ     Northeast    8791894   246
14      New York  NY     Northeast   19378102   517
15  North Dakota  ND North Central     672591     4
16          Ohio  OH North Central   11536504   310
17  Pennsylvania  PA     Northeast   12702379   457
18  Rhode Island  RI     Northeast    1052567    16
19  South Dakota  SD North Central     814180     8
20       Vermont  VT     Northeast     625741     2
21     Wisconsin  WI North Central    5686986    97
table(murders$total)

   2    4    5    7    8   11   12   16   19   21   22   27   32   36   38   53 
   1    1    2    1    1    1    2    1    1    1    1    1    1    1    1    1 
  63   65   67   84   93   97   99  111  116  118  120  135  142  207  219  232 
   1    1    1    1    2    2    1    1    1    1    1    1    1    1    1    1 
 246  250  286  293  310  321  351  364  376  413  457  517  669  805 1257 
   1    1    1    1    1    1    1    1    1    1    1    1    1    1    1 
# Boxplot of total murders by region
murders %>% ggplot(aes(x = region, y = total, fill = region)) + 
  geom_boxplot() + 
  scale_fill_manual(values = c("white", "red", "lightgray", "black"))

data(murders)
p <- mutate(murders, rate = total / population * 100000)
new_data <- filter(p, region != "South")
new_data
           state abb        region population total      rate
1         Alaska  AK          West     710231    19 2.6751860
2        Arizona  AZ          West    6392017   232 3.6295273
3     California  CA          West   37253956  1257 3.3741383
4       Colorado  CO          West    5029196    65 1.2924531
5    Connecticut  CT     Northeast    3574097    97 2.7139722
6         Hawaii  HI          West    1360301     7 0.5145920
7          Idaho  ID          West    1567582    12 0.7655102
8       Illinois  IL North Central   12830632   364 2.8369608
9        Indiana  IN North Central    6483802   142 2.1900730
10          Iowa  IA North Central    3046355    21 0.6893484
11        Kansas  KS North Central    2853118    63 2.2081106
12         Maine  ME     Northeast    1328361    11 0.8280881
13 Massachusetts  MA     Northeast    6547629   118 1.8021791
14      Michigan  MI North Central    9883640   413 4.1786225
15     Minnesota  MN North Central    5303925    53 0.9992600
16      Missouri  MO North Central    5988927   321 5.3598917
17       Montana  MT          West     989415    12 1.2128379
18      Nebraska  NE North Central    1826341    32 1.7521372
19        Nevada  NV          West    2700551    84 3.1104763
20 New Hampshire  NH     Northeast    1316470     5 0.3798036
21    New Jersey  NJ     Northeast    8791894   246 2.7980319
22    New Mexico  NM          West    2059179    67 3.2537239
23      New York  NY     Northeast   19378102   517 2.6679599
24  North Dakota  ND North Central     672591     4 0.5947151
25          Ohio  OH North Central   11536504   310 2.6871225
26        Oregon  OR          West    3831074    36 0.9396843
27  Pennsylvania  PA     Northeast   12702379   457 3.5977513
28  Rhode Island  RI     Northeast    1052567    16 1.5200933
29  South Dakota  SD North Central     814180     8 0.9825837
30          Utah  UT          West    2763885    22 0.7959810
31       Vermont  VT     Northeast     625741     2 0.3196211
32    Washington  WA          West    6724540    93 1.3829942
33     Wisconsin  WI North Central    5686986    97 1.7056487
34       Wyoming  WY          West     563626     5 0.8871131
p %>% ggplot() +
  geom_point(aes(rate, total, col = region), size = 3) +
  geom_text(aes(rate, total, label = abb), nudge_x = 0.05) +
  scale_x_log10() +
  scale_y_log10() +
  xlab("Population In Millions (log10)") +
  ylab("Total Gun Murders (log10)") +
  ggtitle("Total Gun Murders in the US 2010")