---
title: "CHAP 1 - additional exercise"
author: "Valentine"
date: "30/08/2021"
output: pdf_document
---

**Énoncé**  
Read the interro2.rds from the rWSBIM1207 package (version 0.1.9 of later) file into R. The path to the file
can be found with the rWSBIM1207::interro2.rds() function.  
This dataframe provides the scores for 4 tests for 10 students.  
Write a function that calculates the average score for the 3 best tests.  
Calculate this average score for the 10 students.  
This can be done using the apply function or using dplyr functions. For the latter, see rowwise()  

**Chargement des packages et données**
```{r message=FALSE}
library(tidyverse)
library(rWSBIM1207)
rWSBIM1207::interro2.rds()
interro <- readRDS("/usr/local/lib/R/site-library/rWSBIM1207/extdata/interro2.rds")
```

**Générer une fonction qui calcule la moyenne des 3 meilleures notes**
```{r}
mean_3_best <- function(x) {
  m <- mean(x[-which.min(x)],na.rm=TRUE)
  return(m) }
```

**Appliquer cette fonction avec apply()**
```{r}
means <- apply(interro[,-1],1,mean_3_best)
data.frame("noma" = interro$noma,
           "mean of the 3 best interros" = means)
```

**Appliquer cette fonction avec dplyr::rowwise()**
```{r eval=FALSE}
interro %>%
  rowwise() %>%
  mutate("mean of the 3 best interros" = mean_3_best(interro[,-1]))
```
-> error