This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
library(tidyverse)
library(dplyr)
library(ggplot2)
# ---------------------------
# Create Tables
# ---------------------------
burglary <- tibble(
Month = month.name,
`Burglary Reports` = c(12, 7, 9, 3, 4, 11, 6, 10, 8, 5, 13, 12)
)
dallas_part1 <- tibble(
District = c("Central", "Northeast", "Southeast", "Southwest",
"Northwest", "North Central", "South Central"),
`2023 Part I Crimes` = c(7912, 8603, 6744, 6118, 5412, 3986, 6201),
`2024 Part I Crimes` = c(8465, 8129, 7281, 6809, 5597, 4331, 6745)
)
texas_cities <- tibble(
City = c("Houston", "San Antonio", "Dallas", "Austin", "Fort Worth"),
Population = c(2302878, 1472909, 1299544, 979882, 956709),
`Part I Crime Count` = c(118347, 69512, 56188, 39421, 35902)
)
# ---------------------------
# Problem 1
# ---------------------------
burglary_stats <- burglary %>%
mutate(
Frequency = `Burglary Reports`,
Percent = (Frequency / sum(Frequency)) * 100,
Cumulative_Percent = cumsum(Percent)
)
burglary_stats
## # A tibble: 12 × 5
## Month `Burglary Reports` Frequency Percent Cumulative_Percent
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 January 12 12 12 12
## 2 February 7 7 7 19
## 3 March 9 9 9 28
## 4 April 3 3 3 31
## 5 May 4 4 4 35
## 6 June 11 11 11 46
## 7 July 6 6 6 52
## 8 August 10 10 10 62
## 9 September 8 8 8 70
## 10 October 5 5 5 75
## 11 November 13 13 13 88
## 12 December 12 12 12 100
# ---------------------------
# Problem 2
# ---------------------------
dallas_changes <- dallas_part1 %>%
mutate(
Actual_Change = `2024 Part I Crimes` - `2023 Part I Crimes`,
Percent_Change = (Actual_Change / `2023 Part I Crimes`) * 100
)
dallas_changes
## # A tibble: 7 × 5
## District `2023 Part I Crimes` `2024 Part I Crimes` Actual_Change
## <chr> <dbl> <dbl> <dbl>
## 1 Central 7912 8465 553
## 2 Northeast 8603 8129 -474
## 3 Southeast 6744 7281 537
## 4 Southwest 6118 6809 691
## 5 Northwest 5412 5597 185
## 6 North Central 3986 4331 345
## 7 South Central 6201 6745 544
## # ℹ 1 more variable: Percent_Change <dbl>
# ---------------------------
# Problem 3
# ---------------------------
texas_crime_rates <- texas_cities %>%
mutate(
Crime_Rate_per_100k = (`Part I Crime Count` / Population) * 100000
)
texas_crime_rates
## # A tibble: 5 × 4
## City Population `Part I Crime Count` Crime_Rate_per_100k
## <chr> <dbl> <dbl> <dbl>
## 1 Houston 2302878 118347 5139.
## 2 San Antonio 1472909 69512 4719.
## 3 Dallas 1299544 56188 4324.
## 4 Austin 979882 39421 4023.
## 5 Fort Worth 956709 35902 3753.
# ---------------------------
# Problem 4 – Visualizations
# ---------------------------
# Visualization 1: Monthly Burglary Reports
ggplot(burglary, aes(x = Month, y = `Burglary Reports`)) +
geom_bar(stat = "identity") +
labs(title = "Monthly Burglary Reports",
x = "Month",
y = "Number of Reports") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Visualization 2: Crime Rate per 100,000 Residents
ggplot(texas_crime_rates, aes(x = City, y = Crime_Rate_per_100k)) +
geom_bar(stat = "identity") +
labs(title = "Part I Crime Rate per 100,000 Residents",
x = "City",
y = "Crime Rate per 100,000") +
theme_minimal()