Tuesday0929

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(dplyr)
library(ggplot2)
library(dslabs)
data("murders")
murders<-mutate(murders,rate=total/population*100000)
head(murders)
       state abb region population total     rate
1    Alabama  AL  South    4779736   135 2.824424
2     Alaska  AK   West     710231    19 2.675186
3    Arizona  AZ   West    6392017   232 3.629527
4   Arkansas  AR  South    2915918    93 3.189390
5 California  CA   West   37253956  1257 3.374138
6   Colorado  CO   West    5029196    65 1.292453
filter(murders, rate<=0.7)
          state abb        region population total      rate
1        Hawaii  HI          West    1360301     7 0.5145920
2          Iowa  IA North Central    3046355    21 0.6893484
3 New Hampshire  NH     Northeast    1316470     5 0.3798036
4  North Dakota  ND North Central     672591     4 0.5947151
5       Vermont  VT     Northeast     625741     2 0.3196211
no_florida<-filter(murders,state!="Florida")
no_florida
                  state abb        region population total       rate
1               Alabama  AL         South    4779736   135  2.8244238
2                Alaska  AK          West     710231    19  2.6751860
3               Arizona  AZ          West    6392017   232  3.6295273
4              Arkansas  AR         South    2915918    93  3.1893901
5            California  CA          West   37253956  1257  3.3741383
6              Colorado  CO          West    5029196    65  1.2924531
7           Connecticut  CT     Northeast    3574097    97  2.7139722
8              Delaware  DE         South     897934    38  4.2319369
9  District of Columbia  DC         South     601723    99 16.4527532
10              Georgia  GA         South    9920000   376  3.7903226
11               Hawaii  HI          West    1360301     7  0.5145920
12                Idaho  ID          West    1567582    12  0.7655102
13             Illinois  IL North Central   12830632   364  2.8369608
14              Indiana  IN North Central    6483802   142  2.1900730
15                 Iowa  IA North Central    3046355    21  0.6893484
16               Kansas  KS North Central    2853118    63  2.2081106
17             Kentucky  KY         South    4339367   116  2.6732010
18            Louisiana  LA         South    4533372   351  7.7425810
19                Maine  ME     Northeast    1328361    11  0.8280881
20             Maryland  MD         South    5773552   293  5.0748655
21        Massachusetts  MA     Northeast    6547629   118  1.8021791
22             Michigan  MI North Central    9883640   413  4.1786225
23            Minnesota  MN North Central    5303925    53  0.9992600
24          Mississippi  MS         South    2967297   120  4.0440846
25             Missouri  MO North Central    5988927   321  5.3598917
26              Montana  MT          West     989415    12  1.2128379
27             Nebraska  NE North Central    1826341    32  1.7521372
28               Nevada  NV          West    2700551    84  3.1104763
29        New Hampshire  NH     Northeast    1316470     5  0.3798036
30           New Jersey  NJ     Northeast    8791894   246  2.7980319
31           New Mexico  NM          West    2059179    67  3.2537239
32             New York  NY     Northeast   19378102   517  2.6679599
33       North Carolina  NC         South    9535483   286  2.9993237
34         North Dakota  ND North Central     672591     4  0.5947151
35                 Ohio  OH North Central   11536504   310  2.6871225
36             Oklahoma  OK         South    3751351   111  2.9589340
37               Oregon  OR          West    3831074    36  0.9396843
38         Pennsylvania  PA     Northeast   12702379   457  3.5977513
39         Rhode Island  RI     Northeast    1052567    16  1.5200933
40       South Carolina  SC         South    4625364   207  4.4753235
41         South Dakota  SD North Central     814180     8  0.9825837
42            Tennessee  TN         South    6346105   219  3.4509357
43                Texas  TX         South   25145561   805  3.2013603
44                 Utah  UT          West    2763885    22  0.7959810
45              Vermont  VT     Northeast     625741     2  0.3196211
46             Virginia  VA         South    8001024   250  3.1246001
47           Washington  WA          West    6724540    93  1.3829942
48        West Virginia  WV         South    1852994    27  1.4571013
49            Wisconsin  WI North Central    5686986    97  1.7056487
50              Wyoming  WY          West     563626     5  0.8871131
x<-c(2,4,6)
x%in%c(1,2,3,4)
[1]  TRUE  TRUE FALSE
filter(murders,state%in%c("New York","Texas"))
     state abb    region population total    rate
1 New York  NY Northeast   19378102   517 2.66796
2    Texas  TX     South   25145561   805 3.20136
new_table<-select(murders,state,region,rate)
new_table
                  state        region       rate
1               Alabama         South  2.8244238
2                Alaska          West  2.6751860
3               Arizona          West  3.6295273
4              Arkansas         South  3.1893901
5            California          West  3.3741383
6              Colorado          West  1.2924531
7           Connecticut     Northeast  2.7139722
8              Delaware         South  4.2319369
9  District of Columbia         South 16.4527532
10              Florida         South  3.3980688
11              Georgia         South  3.7903226
12               Hawaii          West  0.5145920
13                Idaho          West  0.7655102
14             Illinois North Central  2.8369608
15              Indiana North Central  2.1900730
16                 Iowa North Central  0.6893484
17               Kansas North Central  2.2081106
18             Kentucky         South  2.6732010
19            Louisiana         South  7.7425810
20                Maine     Northeast  0.8280881
21             Maryland         South  5.0748655
22        Massachusetts     Northeast  1.8021791
23             Michigan North Central  4.1786225
24            Minnesota North Central  0.9992600
25          Mississippi         South  4.0440846
26             Missouri North Central  5.3598917
27              Montana          West  1.2128379
28             Nebraska North Central  1.7521372
29               Nevada          West  3.1104763
30        New Hampshire     Northeast  0.3798036
31           New Jersey     Northeast  2.7980319
32           New Mexico          West  3.2537239
33             New York     Northeast  2.6679599
34       North Carolina         South  2.9993237
35         North Dakota North Central  0.5947151
36                 Ohio North Central  2.6871225
37             Oklahoma         South  2.9589340
38               Oregon          West  0.9396843
39         Pennsylvania     Northeast  3.5977513
40         Rhode Island     Northeast  1.5200933
41       South Carolina         South  4.4753235
42         South Dakota North Central  0.9825837
43            Tennessee         South  3.4509357
44                Texas         South  3.2013603
45                 Utah          West  0.7959810
46              Vermont     Northeast  0.3196211
47             Virginia         South  3.1246001
48           Washington          West  1.3829942
49        West Virginia         South  1.4571013
50            Wisconsin North Central  1.7056487
51              Wyoming          West  0.8871131
new_table2<-select(murders,state,population)
new_table2
                  state population
1               Alabama    4779736
2                Alaska     710231
3               Arizona    6392017
4              Arkansas    2915918
5            California   37253956
6              Colorado    5029196
7           Connecticut    3574097
8              Delaware     897934
9  District of Columbia     601723
10              Florida   19687653
11              Georgia    9920000
12               Hawaii    1360301
13                Idaho    1567582
14             Illinois   12830632
15              Indiana    6483802
16                 Iowa    3046355
17               Kansas    2853118
18             Kentucky    4339367
19            Louisiana    4533372
20                Maine    1328361
21             Maryland    5773552
22        Massachusetts    6547629
23             Michigan    9883640
24            Minnesota    5303925
25          Mississippi    2967297
26             Missouri    5988927
27              Montana     989415
28             Nebraska    1826341
29               Nevada    2700551
30        New Hampshire    1316470
31           New Jersey    8791894
32           New Mexico    2059179
33             New York   19378102
34       North Carolina    9535483
35         North Dakota     672591
36                 Ohio   11536504
37             Oklahoma    3751351
38               Oregon    3831074
39         Pennsylvania   12702379
40         Rhode Island    1052567
41       South Carolina    4625364
42         South Dakota     814180
43            Tennessee    6346105
44                Texas   25145561
45                 Utah    2763885
46              Vermont     625741
47             Virginia    8001024
48           Washington    6724540
49        West Virginia    1852994
50            Wisconsin    5686986
51              Wyoming     563626
Maryland<-filter(murders,state=="Maryland")
Maryland
     state abb region population total     rate
1 Maryland  MD  South    5773552   293 5.074866
Maryland_Virgina<-filter(murders,state%in%c("Maryland","Virginia"))
Maryland_Virgina
     state abb region population total     rate
1 Maryland  MD  South    5773552   293 5.074866
2 Virginia  VA  South    8001024   250 3.124600
filter(murders,population>5000000)
            state abb        region population total     rate
1         Arizona  AZ          West    6392017   232 3.629527
2      California  CA          West   37253956  1257 3.374138
3        Colorado  CO          West    5029196    65 1.292453
4         Florida  FL         South   19687653   669 3.398069
5         Georgia  GA         South    9920000   376 3.790323
6        Illinois  IL North Central   12830632   364 2.836961
7         Indiana  IN North Central    6483802   142 2.190073
8        Maryland  MD         South    5773552   293 5.074866
9   Massachusetts  MA     Northeast    6547629   118 1.802179
10       Michigan  MI North Central    9883640   413 4.178622
11      Minnesota  MN North Central    5303925    53 0.999260
12       Missouri  MO North Central    5988927   321 5.359892
13     New Jersey  NJ     Northeast    8791894   246 2.798032
14       New York  NY     Northeast   19378102   517 2.667960
15 North Carolina  NC         South    9535483   286 2.999324
16           Ohio  OH North Central   11536504   310 2.687123
17   Pennsylvania  PA     Northeast   12702379   457 3.597751
18      Tennessee  TN         South    6346105   219 3.450936
19          Texas  TX         South   25145561   805 3.201360
20       Virginia  VA         South    8001024   250 3.124600
21     Washington  WA          West    6724540    93 1.382994
22      Wisconsin  WI North Central    5686986    97 1.705649
filter(murders,population>5000000&total>200)
            state abb        region population total     rate
1         Arizona  AZ          West    6392017   232 3.629527
2      California  CA          West   37253956  1257 3.374138
3         Florida  FL         South   19687653   669 3.398069
4         Georgia  GA         South    9920000   376 3.790323
5        Illinois  IL North Central   12830632   364 2.836961
6        Maryland  MD         South    5773552   293 5.074866
7        Michigan  MI North Central    9883640   413 4.178622
8        Missouri  MO North Central    5988927   321 5.359892
9      New Jersey  NJ     Northeast    8791894   246 2.798032
10       New York  NY     Northeast   19378102   517 2.667960
11 North Carolina  NC         South    9535483   286 2.999324
12           Ohio  OH North Central   11536504   310 2.687123
13   Pennsylvania  PA     Northeast   12702379   457 3.597751
14      Tennessee  TN         South    6346105   219 3.450936
15          Texas  TX         South   25145561   805 3.201360
16       Virginia  VA         South    8001024   250 3.124600
no_south<-filter(murders,region!="South")
no_south
           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
murder_sw<-filter(murders,region%in%c("South","West"))
murder_sw
                  state abb region population total       rate
1               Alabama  AL  South    4779736   135  2.8244238
2                Alaska  AK   West     710231    19  2.6751860
3               Arizona  AZ   West    6392017   232  3.6295273
4              Arkansas  AR  South    2915918    93  3.1893901
5            California  CA   West   37253956  1257  3.3741383
6              Colorado  CO   West    5029196    65  1.2924531
7              Delaware  DE  South     897934    38  4.2319369
8  District of Columbia  DC  South     601723    99 16.4527532
9               Florida  FL  South   19687653   669  3.3980688
10              Georgia  GA  South    9920000   376  3.7903226
11               Hawaii  HI   West    1360301     7  0.5145920
12                Idaho  ID   West    1567582    12  0.7655102
13             Kentucky  KY  South    4339367   116  2.6732010
14            Louisiana  LA  South    4533372   351  7.7425810
15             Maryland  MD  South    5773552   293  5.0748655
16          Mississippi  MS  South    2967297   120  4.0440846
17              Montana  MT   West     989415    12  1.2128379
18               Nevada  NV   West    2700551    84  3.1104763
19           New Mexico  NM   West    2059179    67  3.2537239
20       North Carolina  NC  South    9535483   286  2.9993237
21             Oklahoma  OK  South    3751351   111  2.9589340
22               Oregon  OR   West    3831074    36  0.9396843
23       South Carolina  SC  South    4625364   207  4.4753235
24            Tennessee  TN  South    6346105   219  3.4509357
25                Texas  TX  South   25145561   805  3.2013603
26                 Utah  UT   West    2763885    22  0.7959810
27             Virginia  VA  South    8001024   250  3.1246001
28           Washington  WA   West    6724540    93  1.3829942
29        West Virginia  WV  South    1852994    27  1.4571013
30              Wyoming  WY   West     563626     5  0.8871131