1er quarto

Benjamin Dufossé

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:

1 + 1
[1] 2
#| label: Produit de base
#| echo: false
2 * 2
[1] 4

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

curve(x^2,-5,5)

curve (exp(x),-1,1)

#Travail avec des données

df<-mtcars
names(df)
 [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
[11] "carb"
df$mpg
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
df[,1]
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
df[1:3,1]
[1] 21.0 21.0 22.8
df$hp
 [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215 230  66  52
[20]  65  97 150 150 245 175  66  91 113 264 175 335 109
mean(df$mpg) #moyenne de la variable
[1] 20.09062
sd(df$mpg) 
[1] 6.026948
median(df$mpg) #mediane
[1] 19.2
max(df$mpg) #maximum
[1] 33.9
sort(df$mpg)
 [1] 10.4 10.4 13.3 14.3 14.7 15.0 15.2 15.2 15.5 15.8 16.4 17.3 17.8 18.1 18.7
[16] 19.2 19.2 19.7 21.0 21.0 21.4 21.4 21.5 22.8 22.8 24.4 26.0 27.3 30.4 30.4
[31] 32.4 33.9
hist(df$mpg,col= "red" ,xlab="miles per gallon",ylab="nombre de modele")

rownames (df)
 [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"         
 [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"            
 [7] "Duster 360"          "Merc 240D"           "Merc 230"           
[10] "Merc 280"            "Merc 280C"           "Merc 450SE"         
[13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood" 
[16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"           
[19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"      
[22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"         
[25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"      
[28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"       
[31] "Maserati Bora"       "Volvo 142E"         
table (df$am)

 0  1 
19 13 

Une manière moderne de traiter les données

Le tidyverse install.packages(“tidyverse”)

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.0     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.2     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mtcars|>tibble() # j'ai transformé le df en format tibble
# A tibble: 32 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# ℹ 22 more rows
mtcars|>tibble()|>select(1,2)
# A tibble: 32 × 2
     mpg   cyl
   <dbl> <dbl>
 1  21       6
 2  21       6
 3  22.8     4
 4  21.4     6
 5  18.7     8
 6  18.1     6
 7  14.3     8
 8  24.4     4
 9  22.8     4
10  19.2     6
# ℹ 22 more rows
mtcars|>tibble()|>select(mpg,cyl)|>filter(mpg<15)
# A tibble: 5 × 2
    mpg   cyl
  <dbl> <dbl>
1  14.3     8
2  10.4     8
3  10.4     8
4  14.7     8
5  13.3     8
tbmtcars<-mtcars|>tibble()
tbmtcars|>ggplot(aes(cyl,mpg))+geom_point(col="red")+
  ggtitle("Mpg vs.cylindrée", subtitle = "données mtcars")+xlab("Cylindrée")+ylab("MpG")+
annotate(
  "text",
  x = 6,
  y = 25,
  label = "Des voitures plus raisonnables",
  color = "blue",
  size = 4,
  hjust = 0.5
)

library(ggthemes)
# Diagramme de dispersion avec approximation polynomiale
ggplot(df, aes(x = cyl, y = mpg)) +
  geom_point(color = "red") +
  geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE, color = "blue") +
  labs(
    x = "Nombre de cylindres",
    y = "Consommation (mpg)",
    title = "Évolution du mpg selon le nombre de cylindres"
  ) +
  theme_economist()

Créer des variables