week 3

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

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(rmarkdown)
library(dplyr)
library(ggplot2)
library(dslabs)
library(knitr)
library(tibble)

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

x <- c(1,3,5,4,6,7)
x %in% c(1,2,3,4)
[1]  TRUE  TRUE FALSE  TRUE FALSE FALSE
library(tidyverse)
library(dplyr)
library(ggplot2)
library(dslabs)
data(murders)
murders_new <- mutate(murders,rate_murders=total/population*100000)
head(murders_new)
       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(murders_new,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
no_florida <- filter(murders,state!="Florida")
no_florida
                  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              Georgia  GA         South    9920000   376
11               Hawaii  HI          West    1360301     7
12                Idaho  ID          West    1567582    12
13             Illinois  IL North Central   12830632   364
14              Indiana  IN North Central    6483802   142
15                 Iowa  IA North Central    3046355    21
16               Kansas  KS North Central    2853118    63
17             Kentucky  KY         South    4339367   116
18            Louisiana  LA         South    4533372   351
19                Maine  ME     Northeast    1328361    11
20             Maryland  MD         South    5773552   293
21        Massachusetts  MA     Northeast    6547629   118
22             Michigan  MI North Central    9883640   413
23            Minnesota  MN North Central    5303925    53
24          Mississippi  MS         South    2967297   120
25             Missouri  MO North Central    5988927   321
26              Montana  MT          West     989415    12
27             Nebraska  NE North Central    1826341    32
28               Nevada  NV          West    2700551    84
29        New Hampshire  NH     Northeast    1316470     5
30           New Jersey  NJ     Northeast    8791894   246
31           New Mexico  NM          West    2059179    67
32             New York  NY     Northeast   19378102   517
33       North Carolina  NC         South    9535483   286
34         North Dakota  ND North Central     672591     4
35                 Ohio  OH North Central   11536504   310
36             Oklahoma  OK         South    3751351   111
37               Oregon  OR          West    3831074    36
38         Pennsylvania  PA     Northeast   12702379   457
39         Rhode Island  RI     Northeast    1052567    16
40       South Carolina  SC         South    4625364   207
41         South Dakota  SD North Central     814180     8
42            Tennessee  TN         South    6346105   219
43                Texas  TX         South   25145561   805
44                 Utah  UT          West    2763885    22
45              Vermont  VT     Northeast     625741     2
46             Virginia  VA         South    8001024   250
47           Washington  WA          West    6724540    93
48        West Virginia  WV         South    1852994    27
49            Wisconsin  WI North Central    5686986    97
50              Wyoming  WY          West     563626     5
in_ny_and_tx <- filter(murders,state %in% c("New York","Texas"))
in_ny_and_tx
     state abb    region population total
1 New York  NY Northeast   19378102   517
2    Texas  TX     South   25145561   805
new_table <- select(murders,"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(murders,region!="South")
no_south
           state abb        region population total
1         Alaska  AK          West     710231    19
2        Arizona  AZ          West    6392017   232
3     California  CA          West   37253956  1257
4       Colorado  CO          West    5029196    65
5    Connecticut  CT     Northeast    3574097    97
6         Hawaii  HI          West    1360301     7
7          Idaho  ID          West    1567582    12
8       Illinois  IL North Central   12830632   364
9        Indiana  IN North Central    6483802   142
10          Iowa  IA North Central    3046355    21
11        Kansas  KS North Central    2853118    63
12         Maine  ME     Northeast    1328361    11
13 Massachusetts  MA     Northeast    6547629   118
14      Michigan  MI North Central    9883640   413
15     Minnesota  MN North Central    5303925    53
16      Missouri  MO North Central    5988927   321
17       Montana  MT          West     989415    12
18      Nebraska  NE North Central    1826341    32
19        Nevada  NV          West    2700551    84
20 New Hampshire  NH     Northeast    1316470     5
21    New Jersey  NJ     Northeast    8791894   246
22    New Mexico  NM          West    2059179    67
23      New York  NY     Northeast   19378102   517
24  North Dakota  ND North Central     672591     4
25          Ohio  OH North Central   11536504   310
26        Oregon  OR          West    3831074    36
27  Pennsylvania  PA     Northeast   12702379   457
28  Rhode Island  RI     Northeast    1052567    16
29  South Dakota  SD North Central     814180     8
30          Utah  UT          West    2763885    22
31       Vermont  VT     Northeast     625741     2
32    Washington  WA          West    6724540    93
33     Wisconsin  WI North Central    5686986    97
34       Wyoming  WY          West     563626     5
murders_sw <- filter(murders,region%in% c("South","West"))
murders_sw
                  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              Delaware  DE  South     897934    38
8  District of Columbia  DC  South     601723    99
9               Florida  FL  South   19687653   669
10              Georgia  GA  South    9920000   376
11               Hawaii  HI   West    1360301     7
12                Idaho  ID   West    1567582    12
13             Kentucky  KY  South    4339367   116
14            Louisiana  LA  South    4533372   351
15             Maryland  MD  South    5773552   293
16          Mississippi  MS  South    2967297   120
17              Montana  MT   West     989415    12
18               Nevada  NV   West    2700551    84
19           New Mexico  NM   West    2059179    67
20       North Carolina  NC  South    9535483   286
21             Oklahoma  OK  South    3751351   111
22               Oregon  OR   West    3831074    36
23       South Carolina  SC  South    4625364   207
24            Tennessee  TN  South    6346105   219
25                Texas  TX  South   25145561   805
26                 Utah  UT   West    2763885    22
27             Virginia  VA  South    8001024   250
28           Washington  WA   West    6724540    93
29        West Virginia  WV  South    1852994    27
30              Wyoming  WY   West     563626     5
murders%>%arrange(population)%>%head()
                 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
murders%>%arrange(desc(total))%>%head()
         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
murders %>% ggplot(aes(region,fill=region))+geom_bar()+scale_fill_manual(values=c("red","white","lightgray","darkgray"))

library(tidyverse)
library(dplyr)
library(ggplot2)
library(dslabs)
data(heights)
tall_males <- heights$sex=="Male"&heights$height>70
s <-heights%>%filter(sex=="Female")%>%summarize(average=mean(height),standard_deviation=sd(height))
s
   average standard_deviation
1 64.93942           3.760656
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
class(height_grp)
[1] "grouped_df" "tbl_df"     "tbl"        "data.frame"
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
heights%>%ggplot(aes(sex,height,fill=sex))+geom_boxplot()+scale_fill_manual(values=c("white","red"))