Untitled

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(ggplot2)
library(dslabs)
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
murders1 <- mutate(murders, rate_murders = total / population * 100000)
head(murders1)
       state abb region population total rate_murders
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
murders2 <- filter(murders1, rate_murders <= 0.7)
murders2
          state abb        region population total rate_murders
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
new_table <- select(murders1, state, region)
new_table
                  state        region
1               Alabama         South
2                Alaska          West
3               Arizona          West
4              Arkansas         South
5            California          West
6              Colorado          West
7           Connecticut     Northeast
8              Delaware         South
9  District of Columbia         South
10              Florida         South
11              Georgia         South
12               Hawaii          West
13                Idaho          West
14             Illinois North Central
15              Indiana North Central
16                 Iowa North Central
17               Kansas North Central
18             Kentucky         South
19            Louisiana         South
20                Maine     Northeast
21             Maryland         South
22        Massachusetts     Northeast
23             Michigan North Central
24            Minnesota North Central
25          Mississippi         South
26             Missouri North Central
27              Montana          West
28             Nebraska North Central
29               Nevada          West
30        New Hampshire     Northeast
31           New Jersey     Northeast
32           New Mexico          West
33             New York     Northeast
34       North Carolina         South
35         North Dakota North Central
36                 Ohio North Central
37             Oklahoma         South
38               Oregon          West
39         Pennsylvania     Northeast
40         Rhode Island     Northeast
41       South Carolina         South
42         South Dakota North Central
43            Tennessee         South
44                Texas         South
45                 Utah          West
46              Vermont     Northeast
47             Virginia         South
48           Washington          West
49        West Virginia         South
50            Wisconsin North Central
51              Wyoming          West
no_south <- filter(murders1, region != "South")
no_south
           state abb        region population total rate_murders
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
tall_males <- heights$sex == "Male" & heights$height > 70
head(tall_males)
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE
data(heights)
s <- heights %>% filter (sex == "Female") %>%
  summarize(average = mean(height), standard_deviation = sd (height))
s
   average standard_deviation
1 64.93942           3.760656
s$average
[1] 64.93942
s$standard_deviation
[1] 3.760656
str(s)
'data.frame':   1 obs. of  2 variables:
 $ average           : num 64.9
 $ standard_deviation: num 3.76
height_grp <- heights %>% group_by(sex)
height_grp %>% summarize (avg = mean(height), stdec = sd (height))
# A tibble: 2 × 3
  sex      avg stdec
  <fct>  <dbl> <dbl>
1 Female  64.9  3.76
2 Male    69.3  3.61
table(heights$sex)

Female   Male 
   238    812 
heights %>% ggplot(aes(sex, fill=sex)) + geom_bar()

heights %>% count(sex) %>% mutate(proportion = n/sum(n))
     sex   n proportion
1 Female 238  0.2266667
2   Male 812  0.7733333
data(murders)

murders %>% ggplot(aes(region, fill= region)) + geom_bar() + scale_fill_manual(values=c("red","white", "lightgray", "darkgray"))

heights %>% ggplot(aes(sex, height, fill = sex)) +
  geom_boxplot() +
  scale_fill_manual(values = c("white", "red"))