# load packages
library(tidyverse)Hello this is some text.
casualty_type = c("cat", "dog", "person")
casualty_age = seq(from = 20, to = 60, by = 20)
crashes = data.frame(casualty_type, casualty_age)
plot(crashes$casualty_age)Subsetting.
crashes$casualty_type
crashes[[1]]
crashes[2,1]crashes |>
select(casualty_type)
crashes |>
filter(casualty_age > 35)
crashes |>
filter(casualty_age-20 > 35)
crashes |>
ggplot() +
geom_bar(aes(x = casualty_age, fill = casualty_type))ac = stats19::get_stats19(year = 2020, type = "collision")
class(ac)
dim(ac)
ac_2021 = stats19::get_stats19(year = 2021, type = "collision")
nrow(ac)
nrow(ac_2021)
# # After googling "combine 2 data frames" let's try rbind
# ??combine
# ?rbind
ac = rbind(ac, ac_2021)
dim(ac)
ac_datetime = c(ac$datetime, ac_2021$datetime)
length(ac_datetime)
range(ac_datetime)
class(ac)
str(ac)
names(ac)
# aggregate this by day to show
# how crash numbers varied over the year
ac_by_year = ac |>
group_by(date) |>
summarise(
n_crashes = n()
)
ac_by_year |>
mutate(
`N. crashes per year` = n_crashes,
`Week average` = zoo::rollmean(n_crashes, 7, na.pad = TRUE),
Date = date,
) |>
ggplot(aes(x = Date, y = `N. crashes per year`)) +
geom_point(alpha = 0.1) +
ylim(c(0, NA)) +
# geom_smooth() +
# weekly rolling average
geom_line(aes(Date, `Week average`), colour = "red") +
theme_minimal()# Updated plot with title and legend...
ac_by_year |>
mutate(
`N. crashes per year` = n_crashes,
`Week average` = zoo::rollmean(n_crashes, 7, na.pad = TRUE),
Date = date,
) |>
ggplot(aes(x = Date, y = `N. crashes per year`)) +
geom_point(alpha = 0.1) +
ylim(c(0, NA)) +
# geom_smooth() +
# weekly rolling average
geom_line(aes(Date, `Week average`, colour = "Week average")) +
theme_minimal() +
labs(
colour = "Legend"
) +
scale_colour_manual(values = c("Week average" = "red")) +
ggtitle("Collions/day, 2020 to 2021") +
theme(
legend.position = "bottom"
)1 Python example
casualty_type_py = ["a", "B", "c"]
casualty_type_py