Rpubs

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)
data("murders")
Warning in data("murders"): data set 'murders' not found
data(murders)
Warning in data(murders): data set 'murders' not found
library(dslabs)
data(heights)
data("heights")
x <- c(1, 3, 5)

x %in% c(1, 2, 3, 4)
[1]  TRUE  TRUE FALSE
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 = total/population*100000)
filter(murders1, 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
new_table <- select(murders1, 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
no_south <- filter(murders1, 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
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
tall_males <- heights$sex == "Male" & heights$height>70
head(tall_males)
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE
str(heights)
'data.frame':   1050 obs. of  2 variables:
 $ sex   : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 1 1 1 1 2 ...
 $ height: num  75 70 68 74 61 65 66 62 66 67 ...
head(heights)
     sex height
1   Male     75
2   Male     70
3   Male     68
4   Male     74
5   Male     61
6 Female     65
str(heights)
'data.frame':   1050 obs. of  2 variables:
 $ sex   : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 1 1 1 1 2 ...
 $ height: num  75 70 68 74 61 65 66 62 66 67 ...
tall_makes <- heights$sex== "Male" & heights$height>70
head(tall_males)
[1]  TRUE FALSE FALSE  TRUE FALSE FALSE
s<- heights%>%filter(sex=="Female")%>% 
  summarize(average=mean(height), standard_deviation=sd(height))
s
   average standard_deviation
1 64.93942           3.760656
library(dplyr)
height_grp<- heights%>% group_by(sex)
height_grp %>%summarize(average=mean(height),stdev=(sd(height)))
# A tibble: 2 × 3
  sex    average 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"
data(murders)
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
table(heights$sex)

Female   Male 
   238    812 
table(murders$region)

    Northeast         South North Central          West 
            9            17            12            13 
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
murders%>%ggplot(aes(region,fill=region))+geom_bar()+scale_fill_manual(values=c("pink","blue", "purple","lightblue"))

heights%>%ggplot(aes(sex,height,fill=sex))+geom_boxplot()