---
title: "Boite à Outils R"
author: "Claire Della Vedova"
date: "24/08/2021"
output: 
  html_document : 
    toc : true
    toc_float : true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, 
                      message=FALSE, 
                      eval=FALSE, 
                      results = "hide" )
```


# Importations 

## Importations multiples de fichiers csv

```{r}
files_list <- list.files(here::here("data"),pattern = "*.csv")
library(stringr)
data_names <- str_remove(files_list, ".csv")

for(i in 1: length(files_list)) { 
  x <- read.csv2(here::here("data",files_list[i]))
  assign(data_names[i], x)
    }
rm(x)
```

## Importer plusieurs fichiers csv et les assembler

```{r}
library(dplyr)
library(purrr)
mario_data <- list.files(path = "data2", full.names = TRUE) %>%
    set_names() %>%
    map_dfr(~ read.csv2(.x),.id="file_name") %>% 
    mutate(file_name=str_remove(file_name, "data2/")) %>% 
    mutate(file_name=str_remove(file_name, ".csv"))  
```

# Statistiques

## Tests de normalité

```{r}
apply(iris[,1:4],2, shapiro.test)
```

Pour ne retenir que la pvalue
```{r}
pSW <- function(x)
{
    p <-shapiro.test(x)$p.value
    return(p)
}
apply(iris[,1:4],2, pSW)
```

Avec purrr
```{r}
library(tidyverse)
iris %>% 
    select_if(is.numeric) %>% 
    map(shapiro.test)
```

Avec une fonction personnelle
```{r}
iris %>% 
    select_if(is.numeric) %>% 
    map(pSW)
```

Pour ne garder que les pvalues
```{r}
iris %>% 
    select_if(is.numeric) %>% 
    map(shapiro.test) %>% 
    map("p.value")
```

## Anova à 2 facteurs

```{r}
mod1 <- lm(Usure~Tension * Wool, contrasts=list(Tension=contr.sum, Wool=contr.sum), data=mydata) 

```

