## Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Running Code

When you click the **Render** button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

```{r}
1 + 1
```

You can add options to executable code like this

```{r}
#| echo: false
2 * 2
```

The `echo: false` option disables the printing of code (only output is displayed).

For this quiz, we will use the UK 2024 (provisional) road safety vehicles data available from 

<https://www.data.gov.uk/dataset/cb7ae6f0-4be6-4935-9277-47e5ce24a11f/road-accidents-safety-data>


or [here](https://www.data.gov.uk/dataset/cb7ae6f0-4be6-4935-9277-47e5ce24a11f/road-accidents-safety-data).

Use the following code to read the data in R from a copy of the data at the course website.

<!-- Copy of

https://data.dft.gov.uk/road-accidents-safety-data/dft-road-casualty-statistics-vehicle-provisional-mid-year-unvalidated-2024.csv

-->

```{r}
DATALOC <- "https://deepayan.github.io/BSDS/2025-01-DARP/slides/data/rcs2024.csv"
rcs2024 <- read.csv(DATALOC)
```

Each row in the dataset describes a vehicle involved in a
collision. Multiple vehicles may be involved in a single
collision. The `collision_index` variable is an identifier for the
collision a particular vehicle was involved in.

The `sex_of_driver` variable is coded as an integer, with values 1 =
Male, 2 = Female, 3 = Not known. Missing values are coded as $-1$.


__Question 1__: Identify the **specific** collision _that_ involved the
largest number of vehicles (hint: the number of vehicles involved was
25). For this collision,

```{r}
#| echo: false
veh_per_collision <- xtabs(~ collision_index, rcs2024) |> array2DF()
i <- which.max(veh_per_collision$Value)
max_collision_index <- veh_per_collision[i, "collision_index"]
max_collision_index
```


* How many of the drivers were Male?

```{r,echo=FALSE}
df25 <- dplyr::filter(rcs2024, collision_index == max_collision_index)
tab25 <- xtabs(~ sex_of_driver, df25)
```

```{r}
tab25["1"]
```


* How many of the drivers were Female?

```{r}
tab25["2"]
```


* Ignoring missing values, what was the average age of the Male drivers?

```{r}
df25 |> dplyr::filter(age_of_driver != -1) |> 
  dplyr::group_by(sex_of_driver) |> dplyr::summarise(mean(age_of_driver))
```


* Ignoring missing values, what was the average age of the Female drivers?



