Installer des Packages¶

In [ ]:
# Pour le datascience
library(tidyverse, warn.conflicts = FALSE)
In [4]:
library(readxl)
In [5]:
# Aussi foreign
library(haven, warn.conflicts = FALSE)
In [8]:
library(labelled, warn.conflicts = FALSE)

Tabuler les statistiques¶

In [78]:
# Lire un fichier csv
dat <- read.csv(choose.files())
In [24]:
# Tableaux des fréquence
Freq(dat$size)
A Freq: 3 × 5
levelfreqperccumfreqcumperc
<chr><int><dbl><int><dbl>
Petit64920.3322926 64920.3322926
Moyen64820.3317807129740.6640733
Grand65630.3359267195371.0000000
In [25]:
# Tableau des modilités
table(dat$size)
Petit Moyen Grand 
 6492  6482  6563 
In [26]:
# Tableau trié
Margins(table(dat$size), ord = "desc")
  1. A Freq: 3 × 5
    levelfreqperccumfreqcumperc
    <chr><int><dbl><int><dbl>
    Grand65630.3359267 65630.3359267
    Petit64920.3322926130550.6682193
    Moyen64820.3317807195371.0000000
In [27]:
# Tableau des proportions
prop.table(table(dat$size))
    Petit     Moyen     Grand 
0.3322926 0.3317807 0.3359267 
In [37]:
# Pourcentage
PercTable(size ~ sexe, data = dat, rfrq = "000", margins = c(1,2))
                            
sexe    Homme   Femme    Sum
size                        
Petit   3'261   3'231  6'492
Moyen   3'216   3'266  6'482
Grand   3'292   3'271  6'563
Sum     9'769   9'768 19'537
In [38]:
# Affichage du tableau
tab <- with(dat, table(size, h_pres, sexe))
dimnames(tab) # Les trois dimension
$size
  1. 'Petit'
  2. 'Moyen'
  3. 'Grand'
$h_pres
  1. 'Petit'
  2. 'Grand'
$sexe
  1. 'Homme'
  2. 'Femme'
In [39]:
# Sélection de deux dimension
margin.table(tab, margin = c(1,3))
       sexe
size    Homme Femme
  Petit  3261  3231
  Moyen  3216  3266
  Grand  3292  3271
In [40]:
# Sélection de deux dimension
marginSums(tab, margin = c(1,3))
       sexe
size    Homme Femme
  Petit  3261  3231
  Moyen  3216  3266
  Grand  3292  3271
In [41]:
# Sélection de deux dimension
ftable(tab, row.vars = c(2, 3))
             size Petit Moyen Grand
h_pres sexe                        
Petit  Homme       1162  1166  1158
       Femme       1161  1135  1189
Grand  Homme       2099  2050  2134
       Femme       2070  2131  2082
In [42]:
# Sélection de deux dimentions
ftable(tab, row.vars = c("size", "h_pres"))
             sexe Homme Femme
size  h_pres                 
Petit Petit        1162  1161
      Grand        2099  2070
Moyen Petit        1166  1135
      Grand        2050  2131
Grand Petit        1158  1189
      Grand        2134  2082
In [43]:
# Dans DescTools
Desc(tab, verbose = "high", expected = TRUE, plotit = FALSE)
------------------------------------------------------------------------------ 
tab (table)

Summary: 
n: 19'537, 3-dim table: 3 x 2 x 2

Chi-squared test for independence of all factors:
  X-squared = 3.384, df = 7, p-value = 0.8474

             sexe Homme Femme   Sum
size  h_pres                       
Petit Petit        1162  1161  2323
      Grand        2099  2070  4169
Moyen Petit        1166  1135  2301
      Grand        2050  2131  4181
Grand Petit        1158  1189  2347
      Grand        2134  2082  4216
Sum   Petit        3486  3485  6971
      Grand        6283  6283 12566
In [44]:
# Permutation des dimensions
aperm(tab, c(2,3,1))
, , size = Petit

       sexe
h_pres  Homme Femme
  Petit  1162  1161
  Grand  2099  2070

, , size = Moyen

       sexe
h_pres  Homme Femme
  Petit  1166  1135
  Grand  2050  2131

, , size = Grand

       sexe
h_pres  Homme Femme
  Petit  1158  1189
  Grand  2134  2082
In [45]:
# Sélections de dimensions
Rev(tab, margin = c(1, 3)) #reversing level
, , sexe = Femme

       h_pres
size    Petit Grand
  Grand  1189  2082
  Moyen  1135  2131
  Petit  1161  2070

, , sexe = Homme

       h_pres
size    Petit Grand
  Grand  1158  2134
  Moyen  1166  2050
  Petit  1162  2099
In [46]:
# Zoomer sur la dimension 1
apply(tab, 1, sum)
Petit
6492
Moyen
6482
Grand
6563
In [47]:
# Zoomer sur les dimensions 2 et 3
apply(tab, c(2,3), sum)
A matrix: 2 × 2 of type int
HommeFemme
Petit34863485
Grand62836283
In [48]:
# Zoomer sur les dimension size et h_pres
apply(tab, c("size", "h_pres"), sum)
A matrix: 3 × 2 of type int
PetitGrand
Petit23234169
Moyen23014181
Grand23474216
In [49]:
# Alterner les dimensions
aperm(tab, c(2,3,1))
, , size = Petit

       sexe
h_pres  Homme Femme
  Petit  1162  1161
  Grand  2099  2070

, , size = Moyen

       sexe
h_pres  Homme Femme
  Petit  1166  1135
  Grand  2050  2131

, , size = Grand

       sexe
h_pres  Homme Femme
  Petit  1158  1189
  Grand  2134  2082
In [50]:
# Mettre en forme
CollapseTable(tab, h_pres = c("Léger", "Lourd "), size = c(1,2,2))
, , sexe = Homme

    h_pres
size Léger Lourd 
   1  1162   2099
   2  2324   4184

, , sexe = Femme

    h_pres
size Léger Lourd 
   1  1161   2070
   2  2324   4213
In [51]:
# Tableau de fréquence
(mytab <- xtabs(Freq ~ size + h_pres + sexe, data = tab))
, , sexe = Homme

       h_pres
size    Petit Grand
  Petit  1162  2099
  Moyen  1166  2050
  Grand  1158  2134

, , sexe = Femme

       h_pres
size    Petit Grand
  Petit  1161  2070
  Moyen  1135  2131
  Grand  1189  2082
In [52]:
# Mise en forme
structable(mytab)
            h_pres Petit Grand
size  sexe                    
Petit Homme         1162  2099
      Femme         1161  2070
Moyen Homme         1166  2050
      Femme         1135  2131
Grand Homme         1158  2134
      Femme         1189  2082
In [53]:
# Tableau marginale sur deux dimensions
margin.table(mytab, c(1,2))
       h_pres
size    Petit Grand
  Petit  2323  4169
  Moyen  2301  4181
  Grand  2347  4216
In [58]:
# Proportions marginale sur deux dimensions
(ptab <- prop.table(mytab, c(2,3)))
, , sexe = Homme

       h_pres
size    Petit Grand
  Petit  0.33  0.33
  Moyen  0.33  0.33
  Grand  0.33  0.34

, , sexe = Femme

       h_pres
size    Petit Grand
  Petit  0.33  0.33
  Moyen  0.33  0.34
  Grand  0.34  0.33
In [59]:
# Tableau sur trois dimensions
cb <- array(tab, dim = c(3,3,2))
Abind(cb[,,1], cb[,,2], along = 3)
  1. 1162
  2. 1166
  3. 1158
  4. 2099
  5. 2050
  6. 2134
  7. 1161
  8. 1135
  9. 1189
  10. 2070
  11. 2131
  12. 2082
  13. 1162
  14. 1166
  15. 1158
  16. 2099
  17. 2050
  18. 2134
In [60]:
# Tableaux de fréquences
aggregate(cbind(day, wind)~size, data = dat, FUN = sum)
A data.frame: 3 × 3
sizedaywind
<fct><dbl><dbl>
Petit1e+05325295
Moyen1e+05324780
Grand1e+05327740
In [61]:
# Mise en forme
tab[] <- Format(tab, big.mark = "'", digits = 0, zero.form = "-", 
                na.form = "Missing")
tab[] <- StrAlign(tab, "\\r")
tab
, , sexe = Homme

       h_pres
size    Petit Grand
  Petit 1'162 2'099
  Moyen 1'166 2'050
  Grand 1'158 2'134

, , sexe = Femme

       h_pres
size    Petit Grand
  Petit 1'161 2'070
  Moyen 1'135 2'131
  Grand 1'189 2'082
In [62]:
# Mise en forme
ptab[] <- Format(ptab, digits = 1, fmt = "%")
ptab
, , sexe = Homme

       h_pres
size    Petit Grand
  Petit 33.3% 33.4%
  Moyen 33.4% 32.6%
  Grand 33.2% 34.0%

, , sexe = Femme

       h_pres
size    Petit Grand
  Petit 33.3% 32.9%
  Moyen 32.6% 33.9%
  Grand 34.1% 33.1%

Graphics dans base R¶

In [69]:
# Diagramme en bars
barplot(table(dat$status)[order(table(dat$status))], col = "khaki", las = 2)
No description has been provided for this image
In [70]:
# Bars groupés
with(dat, barplot(table(sexe, status), col = c("khaki","green"), beside = TRUE, legend = TRUE))
No description has been provided for this image
In [71]:
with(dat, boxplot(wind~status))
No description has been provided for this image
In [72]:
plot(wind~day, data = dat)
No description has been provided for this image
In [73]:
hist(dat$wind, main = "L'histogramme")
No description has been provided for this image
In [74]:
# Diagramme circulaire
pie(table(dat$status), col = c("hotpink","khaki","chartreuse"))
No description has been provided for this image