-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsummary_statistics.R
43 lines (34 loc) · 1.15 KB
/
summary_statistics.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
## Summary statistics ##
# Load the necessary packages
library(tidyverse)
# Set your working directory to where the crime data are stored
setwd("../")
# Read the data
crimes <- read_csv("crime_data.csv")
# Frequency of crime by borough (in descending order)
count(crimes, borough, sort = TRUE)
# Frequency of crime category (in descending order)
count(crimes, category, sort = TRUE)
# Frequency and percent of crime category (in descending order)
count(crimes, category, sort = TRUE) %>%
mutate(percent = round(n/sum(n)*100, 1))
# Frequency of crime category by borough (in descending order)
crimes %>%
group_by(category, borough) %>%
summarise(n = n()) %>%
ungroup() %>%
arrange(desc(n))
# Mean frequency of crime category per borough
crimes %>%
group_by(borough, category) %>%
summarise(total = n()) %>%
group_by(category) %>%
summarise(average = round(mean(total, na.rm=TRUE), 0))
# Frequency and percent of Vehicle crime by borough (in descending order)
crimes %>%
filter(category == "Vehicle crime") %>%
group_by(borough) %>%
summarise(n = n()) %>%
ungroup() %>%
arrange(desc(n)) %>%
mutate(percent = round(n/sum(n)*100, 1))