#Importing Data Set

crime=read_csv("~/Documents/Soc 712/Week 2 Tidyverse/Homework/crimedata.csv", col_names = TRUE )
## Parsed with column specification:
## cols(
##   .default = col_character()
## )
## See spec(...) for full column specifications.
head(crime)
## # A tibble: 6 x 35
##   FIPS    `Name of Area`  `Qualifying Name`      Nation   State   County  
##   <chr>   <chr>           <chr>                  <chr>    <chr>   <chr>   
## 1 Geo_FI… Geo_NAME        Geo_QNAME              Geo_NAT… Geo_ST… Geo_COU…
## 2 01001   Autauga County  Autauga County, Alaba… <NA>     01      001     
## 3 04001   Apache County   Apache County, Arizona <NA>     04      001     
## 4 05001   Arkansas County Arkansas County, Arka… <NA>     05      001     
## 5 06001   Alameda County  Alameda County, Calif… <NA>     06      001     
## 6 08001   Adams County    Adams County, Colorado <NA>     08      001     
## # ... with 29 more variables: `Total Population (2016 est.)` <chr>, `Total
## #   Violent and Property Crimes` <chr>, `Total Violent and Property
## #   Crimes: Violent Crimes` <chr>, `Total Violent and Property Crimes:
## #   Property Crimes` <chr>, `Total Violent and Property Crimes
## #   Rate` <chr>, `Total Violent and Property Crimes Rate: Violent Crimes
## #   Rate` <chr>, `Total Violent and Property Crimes Rate: Property Crimes
## #   Rate` <chr>, `Total Violent Crimes` <chr>, `Total Violent Crimes:
## #   Murders and Nonnegligent Manslaughters` <chr>, `Total Violent Crimes:
## #   Rape Revised Definition` <chr>, `Total Violent Crimes: Rape Legacy
## #   Definition` <chr>, `Total Violent Crimes: Robberies` <chr>, `Total
## #   Violent Crimes: Aggravated Assaults` <chr>, `Total Violent Crimes
## #   Rate` <chr>, `Total Violent Crimes Rate: Murders and Nonnegligent
## #   Manslaughters Rate` <chr>, `Total Violent Crimes Rate: Rape Revised
## #   Definition Rate` <chr>, `Total Violent Crimes Rate: Rape Legacy
## #   Definition Rate` <chr>, `Total Violent Crimes Rate: Robberies
## #   Rate` <chr>, `Total Violent Crimes Rate: Aggravated Assaults
## #   Rate` <chr>, `Total Property Crimes` <chr>, `Total Property Crimes:
## #   Burglaries` <chr>, `Total Property Crimes: Larcenies` <chr>, `Total
## #   Property Crimes: Motor Vehicle Thefts` <chr>, `Total Property Crimes
## #   Rate` <chr>, `Total Property Crimes Rate: Burglaries Rate` <chr>,
## #   `Total Property Crimes Rate: Larcenies Rate` <chr>, `Total Property
## #   Crimes Rate: Motor Vehicle Thefts Rate` <chr>, Arsons <chr>, `Arsons
## #   Rate` <chr>
#Removing Duplicated variable names
crime=crime[-1,]
names(crime)
##  [1] "FIPS"                                                                  
##  [2] "Name of Area"                                                          
##  [3] "Qualifying Name"                                                       
##  [4] "Nation"                                                                
##  [5] "State"                                                                 
##  [6] "County"                                                                
##  [7] "Total Population (2016 est.)"                                          
##  [8] "Total Violent and Property Crimes"                                     
##  [9] "Total Violent and Property Crimes: Violent Crimes"                     
## [10] "Total Violent and Property Crimes: Property Crimes"                    
## [11] "Total Violent and Property Crimes Rate"                                
## [12] "Total Violent and Property Crimes Rate: Violent Crimes Rate"           
## [13] "Total Violent and Property Crimes Rate: Property Crimes Rate"          
## [14] "Total Violent Crimes"                                                  
## [15] "Total Violent Crimes: Murders and Nonnegligent Manslaughters"          
## [16] "Total Violent Crimes: Rape Revised Definition"                         
## [17] "Total Violent Crimes: Rape Legacy Definition"                          
## [18] "Total Violent Crimes: Robberies"                                       
## [19] "Total Violent Crimes: Aggravated Assaults"                             
## [20] "Total Violent Crimes Rate"                                             
## [21] "Total Violent Crimes Rate: Murders and Nonnegligent Manslaughters Rate"
## [22] "Total Violent Crimes Rate: Rape Revised Definition Rate"               
## [23] "Total Violent Crimes Rate: Rape Legacy Definition Rate"                
## [24] "Total Violent Crimes Rate: Robberies Rate"                             
## [25] "Total Violent Crimes Rate: Aggravated Assaults Rate"                   
## [26] "Total Property Crimes"                                                 
## [27] "Total Property Crimes: Burglaries"                                     
## [28] "Total Property Crimes: Larcenies"                                      
## [29] "Total Property Crimes: Motor Vehicle Thefts"                           
## [30] "Total Property Crimes Rate"                                            
## [31] "Total Property Crimes Rate: Burglaries Rate"                           
## [32] "Total Property Crimes Rate: Larcenies Rate"                            
## [33] "Total Property Crimes Rate: Motor Vehicle Thefts Rate"                 
## [34] "Arsons"                                                                
## [35] "Arsons Rate"
crime1= select(crime,'Name of Area', 'Qualifying Name', 'State', 'County', 'Total Population (2016 est.)', 'Total Violent Crimes', 'Total Violent Crimes Rate', 'Total Property Crimes', 'Arsons', 'Arsons Rate') %>% arrange(State)
head(crime1)
## # A tibble: 6 x 10
##   `Name of Area` `Qualifying Name`    State County `Total Population (201…
##   <chr>          <chr>                <chr> <chr>  <chr>                  
## 1 Autauga County Autauga County, Ala… 01    001    55416                  
## 2 Baldwin County Baldwin County, Ala… 01    003    208563                 
## 3 Barbour County Barbour County, Ala… 01    005    25965                  
## 4 Bibb County    Bibb County, Alabama 01    007    22643                  
## 5 Blount County  Blount County, Alab… 01    009    57704                  
## 6 Bullock County Bullock County, Ala… 01    011    10362                  
## # ... with 5 more variables: `Total Violent Crimes` <chr>, `Total Violent
## #   Crimes Rate` <chr>, `Total Property Crimes` <chr>, Arsons <chr>,
## #   `Arsons Rate` <chr>
#Average Arson Crime for the United States
summarize(crime1,Arson=mean(as.numeric(Arsons),na.rm = TRUE))
## # A tibble: 1 x 1
##   Arson
##   <dbl>
## 1  3.36
#Selecting New York
NewYorkCrime=filter(crime1,State==36)
#Average Arson Crime for New York
summarize(NewYorkCrime,Arson=mean(as.numeric(Arsons),na.rm = TRUE))
## # A tibble: 1 x 1
##   Arson
##   <dbl>
## 1  4.25
#Nassau Vs. Suffolk
CountyCrime=filter(NewYorkCrime, County=="103"|County=="059"|County=="AAA")
CountyCrime
## # A tibble: 3 x 10
##   `Name of Area`     `Qualifying Name`     State County `Total Population…
##   <chr>              <chr>                 <chr> <chr>  <chr>             
## 1 Nassau County      Nassau County, New Y… 36    059    1361500           
## 2 Suffolk County     Suffolk County, New … 36    103    1492583           
## 3 Suffolk County Po… Suffolk County Polic… 36    AAA    <NA>              
## # ... with 5 more variables: `Total Violent Crimes` <chr>, `Total Violent
## #   Crimes Rate` <chr>, `Total Property Crimes` <chr>, Arsons <chr>,
## #   `Arsons Rate` <chr>
#Merging the two data observations for Suffolk County
CountyCrime[2,8]=CountyCrime[3,8]
CountyCrime[2,9]=CountyCrime[3,9]
CountyCrime
## # A tibble: 3 x 10
##   `Name of Area`     `Qualifying Name`     State County `Total Population…
##   <chr>              <chr>                 <chr> <chr>  <chr>             
## 1 Nassau County      Nassau County, New Y… 36    059    1361500           
## 2 Suffolk County     Suffolk County, New … 36    103    1492583           
## 3 Suffolk County Po… Suffolk County Polic… 36    AAA    <NA>              
## # ... with 5 more variables: `Total Violent Crimes` <chr>, `Total Violent
## #   Crimes Rate` <chr>, `Total Property Crimes` <chr>, Arsons <chr>,
## #   `Arsons Rate` <chr>
#Removing Suffolk Police Dept
CountyCrime=filter(CountyCrime,County!="AAA")
#Only Selecting Arsons for Suffolk and Nassau County
CountyArsons=select(CountyCrime,'Name of Area', Arsons)
CountyArsons
## # A tibble: 2 x 2
##   `Name of Area` Arsons
##   <chr>          <chr> 
## 1 Nassau County  68    
## 2 Suffolk County 109
table(CountyArsons)
##                 Arsons
## Name of Area     109 68
##   Nassau County    0  1
##   Suffolk County   1  0
#Suffolk County had more arsons than Nassau County in 2016