if(!require(tidyverse))install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.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(tidyverse)

CrimeData <- read.csv("https://drkblake.com/wp-content/uploads/2024/02/CrimeData.csv")
head(CrimeData, 5)
##   Description     Incident                   Location              Agency
## 1   Vandalism   24-0000024         600 BLK Dusan Blvd Murfreesboro Police
## 2       Theft   24-0000001    1900 BLK Saddlebrook Dr Murfreesboro Police
## 3     Assault CTN23-036746        1200 BLK BAGWELL DR  Clarksville Police
## 4         DUI   24-0000002 Nw Broad St//N Thompson Ln Murfreesboro Police
## 5         DUI   24-0000006 NW BROAD ST / BRINKLEY AVE Murfreesboro Police
##            Date
## 1 1/1/2024 0:01
## 2 1/1/2024 0:36
## 3 1/1/2024 0:42
## 4 1/1/2024 1:11
## 5 1/1/2024 3:06
dataCrime <- CrimeData
dataCrime <- group_by(dataCrime,Agency,Description)
dataCrime <- summarize(dataCrime, Count = n())
## `summarise()` has grouped output by 'Agency'. You can override using the
## `.groups` argument.
dataCrime <- pivot_wider(dataCrime,
                          names_from = Description, Agency,
                          values_from = Count)
## Warning: Specifying the `id_cols` argument by position was deprecated in tidyr 1.3.0.
## ℹ Please explicitly name `id_cols`, like `id_cols = Agency`.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
head(dataCrime, 17)
## # A tibble: 2 × 15
## # Groups:   Agency [2]
##   Agency Assault   DUI `Disorderly Conduct` Drugs Fraud `Robbery/Burglary` Theft
##   <chr>    <int> <int>                <int> <int> <int>              <int> <int>
## 1 Clark…      29     8                   13    30    11                 12    25
## 2 Murfr…     199    33                   24   126    66                 29   206
## # ℹ 7 more variables: Vandalism <int>, `Weapon violation` <int>, Arson <int>,
## #   `Child Abuse` <int>, Rape <int>, `Sex crime` <int>, Stalking <int>