Commandes c()
, seq()
, rep()
,
paste()
et leurs options.
1:12
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
seq(12)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
seq(1,12)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12
c(0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0)
de trois
manières différentes.seq(from=0.5,to=5,by=0.5)
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
seq(0.5,5,length.out=10)
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
(1:10)/2
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
sort(c(0.5:5,1:5))
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
cumsum(rep(0.5,10))
## [1] 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
cumsum(rep(2,25))
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
(1:25)*2
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
seq(2,50,2)
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
(1:50)[(1:50)%%2==0]
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
sort(rep(0:9,3))
## [1] 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9
rep(0:9,each=3)
## [1] 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 8 8 9 9 9
LETTERS
prédéfinie).x <- rep(LETTERS,1:26)
length(x)
## [1] 351
c("individu 1", "individu 2", ..., "individu 100")
.x <- 1:100
y <- rep("individu",100)
paste(y,x)
## [1] "individu 1" "individu 2" "individu 3" "individu 4" "individu 5"
## [6] "individu 6" "individu 7" "individu 8" "individu 9" "individu 10"
## [11] "individu 11" "individu 12" "individu 13" "individu 14" "individu 15"
## [16] "individu 16" "individu 17" "individu 18" "individu 19" "individu 20"
## [21] "individu 21" "individu 22" "individu 23" "individu 24" "individu 25"
## [26] "individu 26" "individu 27" "individu 28" "individu 29" "individu 30"
## [31] "individu 31" "individu 32" "individu 33" "individu 34" "individu 35"
## [36] "individu 36" "individu 37" "individu 38" "individu 39" "individu 40"
## [41] "individu 41" "individu 42" "individu 43" "individu 44" "individu 45"
## [46] "individu 46" "individu 47" "individu 48" "individu 49" "individu 50"
## [51] "individu 51" "individu 52" "individu 53" "individu 54" "individu 55"
## [56] "individu 56" "individu 57" "individu 58" "individu 59" "individu 60"
## [61] "individu 61" "individu 62" "individu 63" "individu 64" "individu 65"
## [66] "individu 66" "individu 67" "individu 68" "individu 69" "individu 70"
## [71] "individu 71" "individu 72" "individu 73" "individu 74" "individu 75"
## [76] "individu 76" "individu 77" "individu 78" "individu 79" "individu 80"
## [81] "individu 81" "individu 82" "individu 83" "individu 84" "individu 85"
## [86] "individu 86" "individu 87" "individu 88" "individu 89" "individu 90"
## [91] "individu 91" "individu 92" "individu 93" "individu 94" "individu 95"
## [96] "individu 96" "individu 97" "individu 98" "individu 99" "individu 100"
paste("individu",1:100)
## [1] "individu 1" "individu 2" "individu 3" "individu 4" "individu 5"
## [6] "individu 6" "individu 7" "individu 8" "individu 9" "individu 10"
## [11] "individu 11" "individu 12" "individu 13" "individu 14" "individu 15"
## [16] "individu 16" "individu 17" "individu 18" "individu 19" "individu 20"
## [21] "individu 21" "individu 22" "individu 23" "individu 24" "individu 25"
## [26] "individu 26" "individu 27" "individu 28" "individu 29" "individu 30"
## [31] "individu 31" "individu 32" "individu 33" "individu 34" "individu 35"
## [36] "individu 36" "individu 37" "individu 38" "individu 39" "individu 40"
## [41] "individu 41" "individu 42" "individu 43" "individu 44" "individu 45"
## [46] "individu 46" "individu 47" "individu 48" "individu 49" "individu 50"
## [51] "individu 51" "individu 52" "individu 53" "individu 54" "individu 55"
## [56] "individu 56" "individu 57" "individu 58" "individu 59" "individu 60"
## [61] "individu 61" "individu 62" "individu 63" "individu 64" "individu 65"
## [66] "individu 66" "individu 67" "individu 68" "individu 69" "individu 70"
## [71] "individu 71" "individu 72" "individu 73" "individu 74" "individu 75"
## [76] "individu 76" "individu 77" "individu 78" "individu 79" "individu 80"
## [81] "individu 81" "individu 82" "individu 83" "individu 84" "individu 85"
## [86] "individu 86" "individu 87" "individu 88" "individu 89" "individu 90"
## [91] "individu 91" "individu 92" "individu 93" "individu 94" "individu 95"
## [96] "individu 96" "individu 97" "individu 98" "individu 99" "individu 100"
paste("individu",1:100,sep=".")
## [1] "individu.1" "individu.2" "individu.3" "individu.4" "individu.5"
## [6] "individu.6" "individu.7" "individu.8" "individu.9" "individu.10"
## [11] "individu.11" "individu.12" "individu.13" "individu.14" "individu.15"
## [16] "individu.16" "individu.17" "individu.18" "individu.19" "individu.20"
## [21] "individu.21" "individu.22" "individu.23" "individu.24" "individu.25"
## [26] "individu.26" "individu.27" "individu.28" "individu.29" "individu.30"
## [31] "individu.31" "individu.32" "individu.33" "individu.34" "individu.35"
## [36] "individu.36" "individu.37" "individu.38" "individu.39" "individu.40"
## [41] "individu.41" "individu.42" "individu.43" "individu.44" "individu.45"
## [46] "individu.46" "individu.47" "individu.48" "individu.49" "individu.50"
## [51] "individu.51" "individu.52" "individu.53" "individu.54" "individu.55"
## [56] "individu.56" "individu.57" "individu.58" "individu.59" "individu.60"
## [61] "individu.61" "individu.62" "individu.63" "individu.64" "individu.65"
## [66] "individu.66" "individu.67" "individu.68" "individu.69" "individu.70"
## [71] "individu.71" "individu.72" "individu.73" "individu.74" "individu.75"
## [76] "individu.76" "individu.77" "individu.78" "individu.79" "individu.80"
## [81] "individu.81" "individu.82" "individu.83" "individu.84" "individu.85"
## [86] "individu.86" "individu.87" "individu.88" "individu.89" "individu.90"
## [91] "individu.91" "individu.92" "individu.93" "individu.94" "individu.95"
## [96] "individu.96" "individu.97" "individu.98" "individu.99" "individu.100"
Commandes sample
, length
,
sort
, rev
, sum
,
table
, etc.
table
). Renvoyer les indices de la séquence où
l’on trouve la lettre “T”.# Génération :
x <- sample(c("A","T","G","C"),100,replace=TRUE)
# Nombres d'occurences :
y <- (x=="T")
length(y[y])
## [1] 17
length(x[y==TRUE])
## [1] 17
sum(y)
## [1] 17
length(which(y))
## [1] 17
## ou
table(x)
## x
## A C G T
## 24 32 27 17
# Indices :
which(y)
## [1] 3 4 5 7 9 11 21 22 24 28 30 32 36 63 78 84 93
order(y,decreasing=TRUE)[1:sum(y)]
## [1] 3 4 5 7 9 11 21 22 24 28 30 32 36 63 78 84 93
(1:100)[x=="T"]
## [1] 3 4 5 7 9 11 21 22 24 28 30 32 36 63 78 84 93
x
et y
des 100 premiers entiers ordonnés dans
l’ordre croissant et décroissant. Concatenez x
et
y
enlever le seul nombre apparaissant deux fois de suite en
le repérant à l’aide de la commande diff
.## Création du vecteur
vect.ent <- sample(1:100,100,replace=FALSE)
vect.ent
## [1] 99 78 35 82 15 44 93 25 75 86 96 19 91 21 1 18 90 24
## [19] 14 73 17 40 37 54 20 31 51 56 29 57 26 46 34 81 12 89
## [37] 94 27 71 84 16 48 59 67 36 53 65 63 55 87 13 22 38 8
## [55] 28 77 61 69 32 30 88 23 52 70 43 74 10 85 2 100 3 42
## [73] 79 64 97 98 50 6 45 5 60 7 72 39 62 9 33 95 4 68
## [91] 66 76 49 11 92 83 47 58 41 80
## Emplacements min et max
which.min(vect.ent)
## [1] 15
which.max(vect.ent)
## [1] 70
# ou
which(vect.ent==min(vect.ent))
## [1] 15
which(vect.ent==max(vect.ent))
## [1] 70
# Création de x et y
x <- sort(vect.ent)
y <- sort(vect.ent,decreasing=TRUE)
# Concaténation de x et y et transformation
z <- c(x,y)
z[-which(diff(z)==0)]
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## [19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## [37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## [55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## [73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## [91] 91 92 93 94 95 96 97 98 99 100 99 98 97 96 95 94 93 92
## [109] 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74
## [127] 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56
## [145] 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38
## [163] 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20
## [181] 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2
## [199] 1
z[z!=z[diff(z)==0]]
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
## [76] 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 99
## [101] 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74
## [126] 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49
## [151] 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24
## [176] 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
On mesure le taux d’insuline de deux groupes d’individus. Le premier groupe comprend des individus atteints de diabète de type 1 et le deuxième groupe des individus normaux. On observe les valeurs suivantes:
grp1 <- c(14.40 , 13.70 , 14.20 , 17.30 , 13.90 , 13.60 , 15.40 , 10.80 , 12.20 , 13.60)
grp2 <- c(14.00 , 15.90 , 16.90 , 14.10 , 13.80 , 20.30 , 16.00 , 15.30 , 16.10 , 15.90)
mean(grp1); mean(grp2) # moyennes
## [1] 13.91
## [1] 15.83
median(grp1); median(grp2) # médianes
## [1] 13.8
## [1] 15.9
var(grp1); var(grp2) # variances
## [1] 2.985444
## [1] 3.553444
sd(grp1); sd(grp2) # Ecarts-types
## [1] 1.727844
## [1] 1.885058
sqrt(var(grp1)); sqrt(var(grp2)) # Ecarts-types
## [1] 1.727844
## [1] 1.885058
# En utilisant la fonction 'summary' pour extraire moyenne et médiiane :
summary(grp1)[3:4]
## Median Mean
## 13.80 13.91
restemp <- summary(grp2)
restemp[c("Median","Mean")]
## Median Mean
## 15.90 15.83
restemp[c(3,4)]
## Median Mean
## 15.90 15.83
boxplot(grp1,grp2)
On s’intéresse au rendement de champs d’orge traités à différente dose d’engrais et appartenant à différentes variété :
variete <- c("victory", "victory", "victory", "victory", "Golden.rain", "Golden.rain",
"Golden.rain", "Golden.rain", "Marvellous", "Marvellous", "Marvellous",
"Marvellous", "victory", "victory", "victory", "victory",
"Golden.rain", "Golden.rain", "Golden.rain", "Golden.rain", "Marvellous",
"Marvellous", "Marvellous", "Marvellous", "victory", "victory",
"victory", "victory", "Golden.rain", "Golden.rain", "Golden.rain",
"Golden.rain", "Marvellous", "Marvellous", "Marvellous", "Marvellous",
"victory", "victory", "victory", "victory", "Golden.rain", "Golden.rain",
"Golden.rain", "Golden.rain", "Marvellous", "Marvellous", "Marvellous",
"Marvellous", "victory", "victory", "victory", "victory", "Golden.rain",
"Golden.rain", "Golden.rain", "Golden.rain", "Marvellous", "Marvellous",
"Marvellous", "Marvellous", "victory", "victory", "victory", "victory",
"Golden.rain", "Golden.rain", "Golden.rain", "Golden.rain", "Marvellous",
"Marvellous", "Marvellous", "Marvellous")
engrais <- c("0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt",
"0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt",
"0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt",
"0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt",
"0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt",
"0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt",
"0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt",
"0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt",
"0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt",
"0.6cwt", "0.0cwt", "0.2cwt", "0.4cwt", "0.6cwt", "0.0cwt", "0.2cwt",
"0.4cwt", "0.6cwt")
rendement <- c(111, 130, 157, 174, 117, 114, 161, 141, 105, 140, 118, 156, 61, 91, 97,
100, 70, 108, 126, 149, 96, 124, 121, 144, 68, 64, 112, 86, 60, 102, 89,
96, 89, 129, 132, 124, 74, 89, 81, 122, 64, 103, 132, 133, 70, 89, 104,
117, 62, 90, 100, 116, 80, 82, 94, 126, 63, 70, 109, 99, 53, 74, 118, 113,
89, 82, 86, 104, 97, 99, 119, 121)
boxplot
, en découpant par variété, par dose
d’engrais reçu puis par couple variété/dose.variete <- as.factor(variete)
engrais <- as.factor(engrais)
boxplot(split(rendement,variete))
boxplot(split(rendement,engrais))
boxplot(split(rendement,paste(variete,engrais)))
boxplot(split(rendement,data.frame(variete,engrais)))
boxplot(rendement~engrais+variete)
tapply(rendement,variete,mean)
## Golden.rain Marvellous victory
## 104.5000 109.7917 97.6250
tapply(rendement,engrais,mean)
## 0.0cwt 0.2cwt 0.4cwt 0.6cwt
## 79.38889 98.88889 114.22222 123.38889
tapply(rendement,as.factor(paste(variete,engrais)),mean)
## Golden.rain 0.0cwt Golden.rain 0.2cwt Golden.rain 0.4cwt Golden.rain 0.6cwt
## 80.00000 98.50000 114.66667 124.83333
## Marvellous 0.0cwt Marvellous 0.2cwt Marvellous 0.4cwt Marvellous 0.6cwt
## 86.66667 108.50000 117.16667 126.83333
## victory 0.0cwt victory 0.2cwt victory 0.4cwt victory 0.6cwt
## 71.50000 89.66667 110.83333 118.50000
tapply(rendement,variete,summary)
## $Golden.rain
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 60.0 85.0 102.5 104.5 126.0 161.0
##
## $Marvellous
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 63.00 96.75 113.00 109.79 124.00 156.00
##
## $victory
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 53.00 74.00 94.00 97.62 113.75 174.00
tapply(rendement,engrais,summary)
## $`0.0cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 53.00 63.25 72.00 79.39 94.25 117.00
##
## $`0.2cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 64.00 83.75 95.00 98.89 112.50 140.00
##
## $`0.4cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 81.00 97.75 115.00 114.22 124.75 161.00
##
## $`0.6cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 86.0 106.2 121.5 123.4 139.0 174.0
tapply(rendement,as.factor(paste(variete,engrais)),summary)
## $`Golden.rain 0.0cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 60.00 65.50 75.00 80.00 86.75 117.00
##
## $`Golden.rain 0.2cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 82.0 87.0 102.5 98.5 106.8 114.0
##
## $`Golden.rain 0.4cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 86.00 90.25 110.00 114.67 130.50 161.00
##
## $`Golden.rain 0.6cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 96.0 109.5 129.5 124.8 139.0 149.0
##
## $`Marvellous 0.0cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 63.00 74.75 92.50 86.67 96.75 105.00
##
## $`Marvellous 0.2cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 70.0 91.5 111.5 108.5 127.8 140.0
##
## $`Marvellous 0.4cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 104.0 111.2 118.5 117.2 120.5 132.0
##
## $`Marvellous 0.6cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 99.0 118.0 122.5 126.8 139.0 156.0
##
## $`victory 0.0cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 53.00 61.25 65.00 71.50 72.50 111.00
##
## $`victory 0.2cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 64.00 77.75 89.50 89.67 90.75 130.00
##
## $`victory 0.4cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 81.00 97.75 106.00 110.83 116.50 157.00
##
## $`victory 0.6cwt`
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 86.0 103.2 114.5 118.5 120.5 174.0
length(rendement)
## [1] 72
table(variete)
## variete
## Golden.rain Marvellous victory
## 24 24 24
table(engrais)
## engrais
## 0.0cwt 0.2cwt 0.4cwt 0.6cwt
## 18 18 18 18
# ou bien
tapply(rendement,variete,length)
## Golden.rain Marvellous victory
## 24 24 24
tapply(rendement,engrais,length)
## 0.0cwt 0.2cwt 0.4cwt 0.6cwt
## 18 18 18 18
table(as.factor(paste(variete,engrais)))
##
## Golden.rain 0.0cwt Golden.rain 0.2cwt Golden.rain 0.4cwt Golden.rain 0.6cwt
## 6 6 6 6
## Marvellous 0.0cwt Marvellous 0.2cwt Marvellous 0.4cwt Marvellous 0.6cwt
## 6 6 6 6
## victory 0.0cwt victory 0.2cwt victory 0.4cwt victory 0.6cwt
## 6 6 6 6
c. Même question en ne conservant que les champs dont le rendement est
supérieur au rendement moyen total.
length(rendement[rendement>mean(rendement)])
## [1] 35
table(variete[ rendement>mean(rendement) ])
##
## Golden.rain Marvellous victory
## 11 15 9
table(engrais[ rendement>mean(rendement) ])
##
## 0.0cwt 0.2cwt 0.4cwt 0.6cwt
## 3 6 12 14
table ( as.factor(paste(variete,engrais))[ rendement>mean(rendement) ])
##
## Golden.rain 0.0cwt Golden.rain 0.2cwt Golden.rain 0.4cwt Golden.rain 0.6cwt
## 1 2 3 5
## Marvellous 0.0cwt Marvellous 0.2cwt Marvellous 0.4cwt Marvellous 0.6cwt
## 1 3 6 5
## victory 0.0cwt victory 0.2cwt victory 0.4cwt victory 0.6cwt
## 1 1 3 4
d. Quelle est la meilleure combinaison (engrais,variété) en terme de
rendement ? La moins bonne ?
resmean <- tapply(rendement,as.factor(paste(variete,engrais)),mean)
resmean[which.max(resmean)]
## Marvellous 0.6cwt
## 126.8333
vectmean <- tapply(rendement,variete,mean)
table(variete[ (variete==names(vectmean)[1]&rendement>vectmean[1]) |
(variete==names(vectmean)[2]&rendement>vectmean[2]) |
(variete==names(vectmean)[3]&rendement>vectmean[3])])
##
## Golden.rain Marvellous victory
## 10 12 11
# ou bien :
table(variete[rendement>vectmean[variete]])
##
## Golden.rain Marvellous victory
## 10 12 11
data("iris")
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
class(iris)
## [1] "data.frame"
iris2 <- as.matrix(iris[,-5])
class(iris2)
## [1] "matrix" "array"
# Dimension :
dim(iris2)
## [1] 150 4
# Plus grande valeur :
max(iris2)
## [1] 7.9
# Indices ligne et colonne :
which(iris2==max(iris2),arr.ind=TRUE)
## row col
## [1,] 132 1
apply
. Quel individu à la plus grande longueur de Sépale ?
Largeur de Pétale ?apply(iris2,1,mean)
## [1] 2.550 2.375 2.350 2.350 2.550 2.850 2.425 2.525 2.225 2.400 2.700 2.500
## [13] 2.325 2.125 2.800 3.000 2.750 2.575 2.875 2.675 2.675 2.675 2.350 2.650
## [25] 2.575 2.450 2.600 2.600 2.550 2.425 2.425 2.675 2.725 2.825 2.425 2.400
## [37] 2.625 2.500 2.225 2.550 2.525 2.100 2.275 2.675 2.800 2.375 2.675 2.350
## [49] 2.675 2.475 4.075 3.900 4.100 3.275 3.850 3.575 3.975 2.900 3.850 3.300
## [61] 2.875 3.650 3.300 3.775 3.350 3.900 3.650 3.400 3.600 3.275 3.925 3.550
## [73] 3.800 3.700 3.725 3.850 3.950 4.100 3.725 3.200 3.200 3.150 3.400 3.850
## [85] 3.600 3.875 4.000 3.575 3.500 3.325 3.425 3.775 3.400 2.900 3.450 3.525
## [97] 3.525 3.675 2.925 3.475 4.525 3.875 4.525 4.150 4.375 4.825 3.400 4.575
## [109] 4.200 4.850 4.200 4.075 4.350 3.800 4.025 4.300 4.200 5.100 4.875 3.675
## [121] 4.525 3.825 4.800 3.925 4.450 4.550 3.900 3.950 4.225 4.400 4.550 5.025
## [133] 4.250 3.925 3.925 4.775 4.425 4.200 3.900 4.375 4.450 4.350 3.875 4.550
## [145] 4.550 4.300 3.925 4.175 4.325 3.950
apply(iris2,2,mean)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 5.843333 3.057333 3.758000 1.199333
which.max(iris2[,"Sepal.Length"])
## [1] 132
which.max(iris2[,"Petal.Width"])
## [1] 101
pairs
.pairs(iris2)
## ou bien :
# install.packages("GGally")
library("GGally")
## Le chargement a nécessité le package : ggplot2
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
ggpairs(iris,columns=1:4,mapping=ggplot2::aes(color=Species))
microarray <- as.matrix(read.table("https://web.stanford.edu/~hastie/ElemStatLearn/datasets/14cancer.xtrain"))
heatmap
.# covariance
rescov <- cov(microarray)
dim(rescov)
## [1] 144 144
rescov[1:5,1:5]
## V1 V2 V3 V4 V5
## V1 63334.95 48644.56 60965.23 44927.91 67595.95
## V2 48644.56 52315.24 47309.59 41267.38 48883.87
## V3 60965.23 47309.59 220798.03 66972.63 222312.36
## V4 44927.91 41267.38 66972.63 54456.16 79786.49
## V5 67595.95 48883.87 222312.36 79786.49 374021.14
# correlation
rescor <- cor(microarray)
dim(rescor)
## [1] 144 144
rescor[1:5,1:5]
## V1 V2 V3 V4 V5
## V1 1.0000000 0.8450813 0.5155406 0.7650168 0.4391884
## V2 0.8450813 1.0000000 0.4401872 0.7731599 0.3494648
## V3 0.5155406 0.4401872 1.0000000 0.6107679 0.7736026
## V4 0.7650168 0.7731599 0.6107679 1.0000000 0.5590592
## V5 0.4391884 0.3494648 0.7736026 0.5590592 1.0000000
# images
image(rescor)
image(microarray)
heatmap(microarray[1:100,])
On utilise un programme permettant de calculer le nombre d’occurences des 4 nucléotides “A”, “T”, “G” et “C” dans une séquence d’ADN. Celui-ci renvoie une liste comportant 4 éléments, chacun étant un vecteurs décrivant les indices des occurrences des lettres correspondantes.
R
correspondant à l’exécution du programme ci-dessus.# Transformation de la chaîne de caractères en vecteur :
maseq <- unlist(strsplit("AATTCCTCCCGTGACGAAATATA",split=""))
# ou
maseq <- strsplit("AATTCCTCCCGTGACGAAATATA",split="")[[1]]
# Objet correspondant à l'execution du programme
resprog <- list(A=which(maseq=="A"),
T=which(maseq=="T"),
G=which(maseq=="G"),
C=which(maseq=="C"))
b. Déterminer le nombre d'occurences de chaque lettre dans la séquence à partir de cette liste.
sapply(resprog,length)
## A T G C
## 8 6 3 6
lapply(resprog,length)
## $A
## [1] 8
##
## $T
## [1] 6
##
## $G
## [1] 3
##
## $C
## [1] 6
mafonction <- function(masequence){
maseq <- unlist(strsplit(masequence,split=""))
resprog <- list(A=which(maseq=="A"),
T=which(maseq=="T"),
G=which(maseq=="G"),
C=which(maseq=="C"))
return(resprog)
}
# test fonction : mafonction("AATTCCTCCCGTGACGAAATATA")
# Création de la liste à 3 entrées :
messequences <- c("ATTCG","CCGT","GCGAGG")
biglist <- list(mafonction("ATTCG"), mafonction("CCGT"), mafonction("GCGAGG"))
# ou bien
biglist <- lapply(messequences,mafonction)
# ou bien
biglist <- list()
for(i in 1:length(messequences)){
biglist[[i]] <- mafonction(messequences[i])
}
# ou
sapply(biglist,function(x){sum(sapply(x,length))})
## [1] 5 4 6
# c)
b. Déterminer la longueur de chaque séquence à partir de cette liste
longueur.sequences <- rep(NA,length(biglist))
for(i in (1:length(biglist))){
longueur.sequences[i] <- sum(sapply(biglist[[i]],length))
}
longueur.sequences
## [1] 5 4 6
# ou
sapply(biglist,function(x){sum(sapply(x,length))})
## [1] 5 4 6
c. Déterminer le nombre d'occurences de chaque nucléotide dans chacune des listes. Renvoyer le résultat sous forme de matrice 3 x 4 (on pourra s'aider de la fonction `sapply`).
t(sapply(biglist,function(x){(sapply(x,length))}))
## A T G C
## [1,] 1 2 1 1
## [2,] 0 1 1 2
## [3,] 1 0 4 1
diamonds
de la librairie
ggplot2
(commande :
library(ggplot2); data(diamonds)
). Vérifier qu’il s’agit
bien d’un data.frame
. Déterminer les noms des variables
considérées et leur nature. Faire un résumé numérique.library(ggplot2)
data(diamonds)
?diamonds
head(diamonds)
## # A tibble: 6 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.2 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
# Vérification data.frame
class(diamonds)
## [1] "tbl_df" "tbl" "data.frame"
is.data.frame((diamonds))
## [1] TRUE
# noms des variables et nature :
names(diamonds)
## [1] "carat" "cut" "color" "clarity" "depth" "table" "price"
## [8] "x" "y" "z"
colnames(diamonds)
## [1] "carat" "cut" "color" "clarity" "depth" "table" "price"
## [8] "x" "y" "z"
sapply(diamonds,class)
## $carat
## [1] "numeric"
##
## $cut
## [1] "ordered" "factor"
##
## $color
## [1] "ordered" "factor"
##
## $clarity
## [1] "ordered" "factor"
##
## $depth
## [1] "numeric"
##
## $table
## [1] "numeric"
##
## $price
## [1] "integer"
##
## $x
## [1] "numeric"
##
## $y
## [1] "numeric"
##
## $z
## [1] "numeric"
# ou
str(diamonds)
## tibble [53,940 × 10] (S3: tbl_df/tbl/data.frame)
## $ carat : num [1:53940] 0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
## $ cut : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
## $ color : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
## $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
## $ depth : num [1:53940] 61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
## $ table : num [1:53940] 55 61 65 58 58 57 57 55 61 61 ...
## $ price : int [1:53940] 326 326 327 334 335 336 336 337 337 338 ...
## $ x : num [1:53940] 3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
## $ y : num [1:53940] 3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
## $ z : num [1:53940] 2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
# Résumé numérique
summary(diamonds)
## carat cut color clarity depth
## Min. :0.2000 Fair : 1610 D: 6775 SI1 :13065 Min. :43.00
## 1st Qu.:0.4000 Good : 4906 E: 9797 VS2 :12258 1st Qu.:61.00
## Median :0.7000 Very Good:12082 F: 9542 SI2 : 9194 Median :61.80
## Mean :0.7979 Premium :13791 G:11292 VS1 : 8171 Mean :61.75
## 3rd Qu.:1.0400 Ideal :21551 H: 8304 VVS2 : 5066 3rd Qu.:62.50
## Max. :5.0100 I: 5422 VVS1 : 3655 Max. :79.00
## J: 2808 (Other): 2531
## table price x y
## Min. :43.00 Min. : 326 Min. : 0.000 Min. : 0.000
## 1st Qu.:56.00 1st Qu.: 950 1st Qu.: 4.710 1st Qu.: 4.720
## Median :57.00 Median : 2401 Median : 5.700 Median : 5.710
## Mean :57.46 Mean : 3933 Mean : 5.731 Mean : 5.735
## 3rd Qu.:59.00 3rd Qu.: 5324 3rd Qu.: 6.540 3rd Qu.: 6.540
## Max. :95.00 Max. :18823 Max. :10.740 Max. :58.900
##
## z
## Min. : 0.000
## 1st Qu.: 2.910
## Median : 3.530
## Mean : 3.539
## 3rd Qu.: 4.040
## Max. :31.800
##
# Chemin de recherche
attach(diamonds)
## Les objets suivants sont masqués _par_ .GlobalEnv:
##
## x, y, z
Extraire les entrées du tableau telles que
Premium
diamonds2 <- subset(diamonds,cut=="Premium")
# ou
diamonds2 <- diamonds[diamonds$cut=="Premium",]
# ou
attach(diamonds) # !!!! A ne pas oublier avant d'exécuter les lignes qui suivent pour que 'cut' soit reconnu!!!!
## Les objets suivants sont masqués _par_ .GlobalEnv:
##
## x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
diamonds2 <- diamonds[cut=="Premium",]
+ le carat soit supérieur à 3
attach(diamonds)
## Les objets suivants sont masqués _par_ .GlobalEnv:
##
## x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
diamonds3 <- subset(diamonds,carat>3)
diamonds3 <- diamonds[carat>3,]
+ le volume (approximatif) soit supérieur à \(500 mm^3\)
rm(x);rm(y)
attach(diamonds)
## L'objet suivant est masqué _par_ .GlobalEnv:
##
## z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 5):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
diamonds3 <- subset(diamonds,x*y*z>500)
diamonds3 <- diamonds[x*y*z>500,]
## Warning in x * y * z: la taille d'un objet plus long n'est pas multiple de la
## taille d'un objet plus court
#ou
diamonds$volume <- x*y*z
## Warning in x * y * z: la taille d'un objet plus long n'est pas multiple de la
## taille d'un objet plus court
attach(diamonds)
## L'objet suivant est masqué _par_ .GlobalEnv:
##
## z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 5):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 6):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
diamonds3 <- diamonds[volume>500,]
+ la qualité soit idéale, le prix inférieur à 1000 et le carat supérieur à .5. Déterminer la répartition des couleurs pour ce sous-ensemble
attach(diamonds)
## L'objet suivant est masqué _par_ .GlobalEnv:
##
## z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 5):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 6):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 7):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
diamonds3 <- subset(diamonds,cut=="Ideal"&price<=1000&carat>=0.5)
diamonds3 <- diamonds[cut=="Ideal"&price<=1000&carat>=0.5,]
dim(diamonds3)
## [1] 41 11
# répartition des couleurs
table(diamonds3[,"color"])
## color
## D E F G H I J
## 1 0 0 1 9 15 15
table(diamonds3[,3])
## color
## D E F G H I J
## 1 0 0 1 9 15 15
table(diamonds3$color)
##
## D E F G H I J
## 1 0 0 1 9 15 15
table(subset(diamonds3,select=color))
## color
## D E F G H I J
## 1 0 0 1 9 15 15
cut
).attach(diamonds)
## L'objet suivant est masqué _par_ .GlobalEnv:
##
## z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 5):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 6):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 7):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 8):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
tapply(price,cut,mean)
## Fair Good Very Good Premium Ideal
## 4358.758 3928.864 3981.760 4584.258 3457.542
diamonds$intervallesdecarats <- cut(carat,breaks=6)
tapply(diamonds$price,diamonds$intervallesdecarats,mean)
## (0.195,1] (1,1.8] (1.8,2.6] (2.6,3.41] (3.41,4.21] (4.21,5.01]
## 1786.951 7455.363 14731.454 14390.509 15363.500 18274.500
attach(diamonds)
## L'objet suivant est masqué _par_ .GlobalEnv:
##
## z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 5):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 6):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 7):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 8):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 9):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
plot(price,volume,pch=19,cex=.5,col="blue",main="volume en fonction du prix",ylim=c(0,500))
plot(volume~price)
boxplot(carat~cut)
# avec ggplot :
# install.packages("gplots")
library(ggplot2)
ggplot(diamonds, aes(x=price, y=volume)) +
geom_point()
ggplot(diamonds,
aes(x=price,y=volume,color=cut,shape=cut)) +
geom_point()
## Warning: Using shapes for an ordinal variable is not advised
ggplot(diamonds, aes(x=price, y=volume, color=cut)) +
geom_point() +
geom_smooth(method=lm)
## `geom_smooth()` using formula 'y ~ x'
ggplot(diamonds, aes(x=price, y=volume, color=cut)) +
geom_point() +
geom_smooth(method=lm) +
coord_cartesian( ylim = c(0, 600))
## `geom_smooth()` using formula 'y ~ x'
attach(diamonds)
## L'objet suivant est masqué _par_ .GlobalEnv:
##
## z
## Les objets suivants sont masqués depuis diamonds (pos = 3):
##
## carat, clarity, color, cut, depth, intervallesdecarats, price,
## table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 4):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 5):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 6):
##
## carat, clarity, color, cut, depth, price, table, volume, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 7):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 8):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 9):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
## Les objets suivants sont masqués depuis diamonds (pos = 10):
##
## carat, clarity, color, cut, depth, price, table, x, y, z
tapply(price,factor(paste(cut,color,clarity)),mean)
## Fair D I1 Fair D IF Fair D SI1 Fair D SI2
## 7383.000 1619.667 4273.345 4355.143
## Fair D VS1 Fair D VS2 Fair D VVS1 Fair D VVS2
## 2921.200 4512.880 4473.000 3607.000
## Fair E I1 Fair E SI1 Fair E SI2 Fair E VS1
## 2095.222 3901.154 4172.385 3307.929
## Fair E VS2 Fair E VVS1 Fair E VVS2 Fair F I1
## 3041.714 4115.333 3119.308 2543.514
## Fair F IF Fair F SI1 Fair F SI2 Fair F VS1
## 2344.000 3784.687 4520.112 4103.061
## Fair F VS2 Fair F VVS1 Fair F VVS2 Fair G I1
## 3400.472 4679.800 4018.200 3187.472
## Fair G IF Fair G SI1 Fair G SI2 Fair G VS1
## 1488.000 3579.362 5665.150 3497.622
## Fair G VS2 Fair G VVS1 Fair G VVS2 Fair H I1
## 5384.444 2216.333 3099.059 4212.962
## Fair H SI1 Fair H SI2 Fair H VS1 Fair H VS2
## 5195.800 6022.407 4604.750 5110.927
## Fair H VVS1 Fair H VVS2 Fair I I1 Fair I SI1
## 4115.000 3481.727 3501.000 4574.967
## Fair I SI2 Fair I VS1 Fair I VS2 Fair I VVS1
## 6658.022 4500.480 3856.125 4194.000
## Fair I VVS2 Fair J I1 Fair J SI1 Fair J SI2
## 2994.625 5795.043 4553.929 5131.815
## Fair J VS1 Fair J VS2 Fair J VVS1 Fair J VVS2
## 5906.188 4067.826 1691.000 2998.000
## Good D I1 Good D IF Good D SI1 Good D SI2
## 3490.750 10030.333 3021.173 3595.296
## Good D VS1 Good D VS2 Good D VVS1 Good D VVS2
## 3556.581 3588.462 2586.231 2345.640
## Good E I1 Good E IF Good E SI1 Good E SI2
## 4398.130 1519.222 3162.132 3785.490
## Good E VS1 Good E VS2 Good E VVS1 Good E VVS2
## 3712.775 3772.019 1905.953 3390.154
## Good F I1 Good F IF Good F SI1 Good F SI2
## 2569.526 3132.867 3261.454 4426.786
## Good F VS1 Good F VS2 Good F VVS1 Good F VVS2
## 2787.508 3790.543 2189.514 3192.360
## Good G I1 Good G IF Good G SI1 Good G SI2
## 3195.789 4060.136 4129.329 4776.411
## Good G VS1 Good G VS2 Good G VVS1 Good G VVS2
## 4302.428 4140.714 2705.195 3310.467
## Good H I1 Good H IF Good H SI1 Good H SI2
## 3849.714 5948.750 4179.285 5529.778
## Good H VS1 Good H VS2 Good H VVS1 Good H VVS2
## 3819.117 4433.043 1719.710 2428.000
## Good I I1 Good I IF Good I SI1 Good I SI2
## 4175.444 1749.333 4742.945 6933.012
## Good I VS1 Good I VS2 Good I VVS1 Good I VVS2
## 4597.165 5956.564 2650.955 2758.000
## Good J I1 Good J IF Good J SI1 Good J SI2
## 3794.500 2738.000 4627.625 5306.113
## Good J VS1 Good J VS2 Good J VVS1 Good J VVS2
## 3662.827 4803.167 4633.000 4371.154
## Ideal D I1 Ideal D IF Ideal D SI1 Ideal D SI2
## 3526.923 6567.179 2490.459 3142.048
## Ideal D VS1 Ideal D VS2 Ideal D VVS1 Ideal D VVS2
## 2576.040 2111.927 2705.778 3619.014
## Ideal E I1 Ideal E IF Ideal E SI1 Ideal E SI2
## 3559.389 3258.937 2883.808 3891.303
## Ideal E VS1 Ideal E VS2 Ideal E VVS1 Ideal E VVS2
## 2175.798 2163.324 2205.519 2556.335
## Ideal F I1 Ideal F IF Ideal F SI1 Ideal F SI2
## 3903.452 2153.709 3710.322 4335.508
## Ideal F VS1 Ideal F VS2 Ideal F VVS1 Ideal F VVS2
## 3504.002 3317.205 2611.234 3323.629
## Ideal G I1 Ideal G IF Ideal G SI1 Ideal G SI2
## 4044.438 2206.031 3441.108 4612.086
## Ideal G VS1 Ideal G VS2 Ideal G VVS1 Ideal G VVS2
## 4116.918 4310.035 2909.199 3795.651
## Ideal H I1 Ideal H IF Ideal H SI1 Ideal H SI2
## 5415.184 1982.765 4769.988 5589.473
## Ideal H VS1 Ideal H VS2 Ideal H VVS1 Ideal H VVS2
## 3613.325 4039.126 1915.985 2591.156
## Ideal I I1 Ideal I IF Ideal I SI1 Ideal I SI2
## 4103.294 1502.621 5178.565 7191.912
## Ideal I VS1 Ideal I VS2 Ideal I VVS1 Ideal I VVS2
## 3944.422 4663.384 2034.397 2858.680
## Ideal J I1 Ideal J IF Ideal J SI1 Ideal J SI2
## 9454.000 2489.000 5115.675 6555.173
## Ideal J VS1 Ideal J VS2 Ideal J VVS1 Ideal J VVS2
## 4734.428 4867.134 2000.172 4121.926
## Premium D I1 Premium D IF Premium D SI1 Premium D SI2
## 3818.750 9056.500 3236.378 4351.086
## Premium D VS1 Premium D VS2 Premium D VVS1 Premium D VVS2
## 4178.046 2919.357 3771.000 3888.436
## Premium E I1 Premium E IF Premium E SI1 Premium E SI2
## 3199.267 4525.444 3362.625 4489.931
## Premium E VS1 Premium E VS2 Premium E VVS1 Premium E VVS2
## 3721.695 3070.394 2699.857 2940.942
## Premium F I1 Premium F IF Premium F SI1 Premium F SI2
## 3554.559 3617.581 4040.467 4747.090
## Premium F VS1 Premium F VS2 Premium F VVS1 Premium F VVS2
## 4758.038 4221.467 3969.325 4099.466
## Premium G I1 Premium G IF Premium G SI1 Premium G SI2
## 4051.522 3311.115 4303.348 5617.205
## Premium G VS1 Premium G VS2 Premium G VVS1 Premium G VVS2
## 4435.823 4556.255 2933.655 4323.571
## Premium H I1 Premium H IF Premium H SI1 Premium H SI2
## 3904.348 3384.750 5707.722 6718.946
## Premium H VS1 Premium H VS2 Premium H VVS1 Premium H VVS2
## 3949.336 5553.876 1453.759 2651.263
## Premium I I1 Premium I IF Premium I SI1 Premium I SI2
## 5044.625 2358.565 6092.093 7148.484
## Premium I VS1 Premium I VS2 Premium I VVS1 Premium I VVS2
## 5339.367 7156.346 1831.083 3190.768
## Premium J I1 Premium J IF Premium J SI1 Premium J SI2
## 4577.231 7026.000 5726.579 7550.286
## Premium J VS1 Premium J VS2 Premium J VVS1 Premium J VVS2
## 5817.261 6175.559 7244.375 6423.353
## Very Good D I1 Very Good D IF Very Good D SI1 Very Good D SI2
## 2622.800 10298.261 3234.931 4425.459
## Very Good D VS1 Very Good D VS2 Very Good D VVS1 Very Good D VVS2
## 2955.480 3145.194 2987.731 2615.298
## Very Good E I1 Very Good E IF Very Good E SI1 Very Good E SI2
## 3443.545 4332.744 3228.176 4279.447
## Very Good E VS1 Very Good E VS2 Very Good E VVS1 Very Good E VVS2
## 3089.358 3329.497 1997.447 2041.685
## Very Good F I1 Very Good F IF Very Good F SI1 Very Good F SI2
## 4252.923 4677.075 3574.292 4249.758
## Very Good F VS1 Very Good F VS2 Very Good F VVS1 Very Good F VVS2
## 3880.802 3995.944 2826.540 3461.912
## Very Good G I1 Very Good G IF Very Good G SI1 Very Good G SI2
## 3194.812 3525.241 3481.871 4699.269
## Very Good G VS1 Very Good G VS2 Very Good G VVS1 Very Good G VVS2
## 3770.150 4426.816 2719.332 3711.785
## Very Good H I1 Very Good H IF Very Good H SI1 Very Good H SI2
## 5258.833 2647.690 4933.945 6112.414
## Very Good H VS1 Very Good H VS2 Very Good H VVS1 Very Good H VVS2
## 3750.198 4620.221 2042.191 2768.145
## Very Good I I1 Very Good I IF Very Good I SI1 Very Good I SI2
## 6045.125 4093.895 5195.302 6621.600
## Very Good I VS1 Very Good I VS2 Very Good I VVS1 Very Good I VVS2
## 5276.971 5754.642 2056.420 3059.887
## Very Good J I1 Very Good J IF Very Good J SI1 Very Good J SI2
## 4478.375 1074.125 5026.544 5992.898
## Very Good J VS1 Very Good J VS2 Very Good J VVS1 Very Good J VVS2
## 4339.592 5325.549 3175.526 5960.448
fonction9.1 <- function(x){
res <- sin(x)^2+sqrt(abs(x-3))
return(res)
}
fonction9.1(0) # exécution de la fonction pour x=0
## [1] 1.732051
curve(fonction9.1,-6,3)
plot(fonction9.1,xlim=c(-6,3))
fonction9.3 <- function(x){
if(x>0){
res <- sin(x)^2*log(x)
} else {
res <- sin(x)^2*x
}
return(res)
}
# ou
fonction9.3 <- function(x){
res <- ifelse(x>0,sin(x)^2*log(x),sin(x)^2*x)
return(res)
}
fonction9.3(4) # Execution de la fonction pour x=4
## [1] 0.7940001
# Graphe :
curve(fonction9.3,-6,3)
## Warning in log(x): Production de NaN
La formule du calcul de l’indice de masse corporelle (IMC) est la suivante : \[IMC=\frac{poids(kg)}{taille(m)^2}\] l’IMC permet d’évaluer les risques liés à un surpoids chez l’adulte :
## ICM (kg/m^2) Classification Risque
## [1,] "<18.5" "Poids insuffisant" "Accru"
## [2,] "18.5 à 24.9" "Poids normal" "Moindre"
## [3,] "25 à 29.9" "Surpoids" "Accru"
## [4,] ">30" "Obésité" "Elevé"
calculIMC <- function(poids,taille){
res <- poids/taille^2
return(res)
}
Calculer l’\(IMC\) d’une personne :
calculIMC(64,1.64)
## [1] 23.79536
calculIMC(56,1.61)
## [1] 21.6041
calculIMC(102,1.72)
## [1] 34.4781
calculIMC(51,1.65)
## [1] 18.73278
classifIMC2 <- function(taille,poids){
res0 <- calculIMC(poids,taille)
if(res0 < 18.5){
res <- "Poids insuffisant"
}
if(res0 >= 18.5 & res0 <= 24.9) {
res <- "Poids normal"
}
if(res0 >=25 & res0 <= 29.9){
res <- "Surpoids"
}
if(res0 > 30){
res <- "Obésité"
}
return(res)
}
resclassification <- classifIMC2(1.64,64)
classifIMC2(1.64,64)
## [1] "Poids normal"
classifIMC2(1.61,56)
## [1] "Poids normal"
classifIMC2(1.72,102)
## [1] "Obésité"
classifIMC2(1.65,51)
## [1] "Poids normal"