Tableaux de fréquences, transposition
Bonjour,
Pourquoi ne pas calculer les pourcentages avec les fonctions table() et prop.table() :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| tab <- read.table(header = TRUE, stringsAsFactors = TRUE, text = "
Verif Taille
Lundi1 28
Lundi2 12
Lundi2 3
Lundi2 5
NA 1
Lundi1 1
Lundi1 4
Jeudi1 1
Lundi1 18
Lundi1 1
") |
Code:
1 2 3 4 5 6 7 8 9 10 11
| tab$Taille_c <- cut(tab$Taille,breaks=c(0,11,20,30))
tab_freq <- table(tab$Verif, tab$Taille_c, useNA = "ifany", deparse.level=2)
round(prop.table(tab_freq,1),2)
#> tab$Taille_c
#> tab$Verif (0,11] (11,20] (20,30]
#> Jeudi1 1.00 0.00 0.00
#> Lundi1 0.60 0.20 0.20
#> Lundi2 0.67 0.33 0.00
#> <NA> 1.00 0.00 0.00
# Created on 2021-02-26 by the reprex package (v0.3.0.9001) |
Quant à votre question concernant la transposition, vous pouvez utiliser la fonction dcast() du package reshape2 :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| tab <- read.table(header = TRUE, text = "
size Verif n pct
(0,11] Lundi1 10 7.04
(0,11] Mardi1 10 7.04
(0,11] NA 122 85.9
(11,20] Lundi1 1 4.17
(11,20] Mardi1 1 4.17
(11,20] NA 22 91.7
(20,30] Mardi1 3 14.3
(20,30] NA 18 85.7
(30,40] Lundi1 1 11.1
(30,40] NA 8 88.9
(40,50] Mardi1 1 8.33
(40,50] NA 11 91.7
(50,60] Lundi1 2 22.2
(50,60] NA 7 77.8
(60,300] Jeudi1 5 11.9
(60,300] NA 37 88.1
") |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| pct <- reshape2::dcast(tab, Verif ~ size, fill = 0, value.var="pct")
nb <- reshape2::dcast(tab, Verif ~ size, fill = 0, value.var="n")
total <- cbind(Verif="Nombre", as.data.frame(t(colSums(nb[,-1]))))
pct <- rbind(pct, total)
pct
#> Verif (0,11] (11,20] (20,30] (30,40] (40,50] (50,60] (60,300]
#> 1 Jeudi1 0.00 0.00 0.0 0.0 0.00 0.0 11.9
#> 2 Lundi1 7.04 4.17 0.0 11.1 0.00 22.2 0.0
#> 3 Mardi1 7.04 4.17 14.3 0.0 8.33 0.0 0.0
#> 4 <NA> 85.90 91.70 85.7 88.9 91.70 77.8 88.1
#> 5 Nombre 142.00 24.00 21.0 9.0 12.00 9.0 42.0
# Created on 2021-02-26 by the reprex package (v0.3.0.9001) |
Cordialement,