#Chapitre 2
#page 22
data(package="datasets")
?iris   

#page 23
help(iris)
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
## 7            4.6         3.4          1.4         0.3     setosa
## 8            5.0         3.4          1.5         0.2     setosa
## 9            4.4         2.9          1.4         0.2     setosa
## 10           4.9         3.1          1.5         0.1     setosa
## 11           5.4         3.7          1.5         0.2     setosa
## 12           4.8         3.4          1.6         0.2     setosa
## 13           4.8         3.0          1.4         0.1     setosa
## 14           4.3         3.0          1.1         0.1     setosa
## 15           5.8         4.0          1.2         0.2     setosa
## 16           5.7         4.4          1.5         0.4     setosa
## 17           5.4         3.9          1.3         0.4     setosa
## 18           5.1         3.5          1.4         0.3     setosa
## 19           5.7         3.8          1.7         0.3     setosa
## 20           5.1         3.8          1.5         0.3     setosa
## 21           5.4         3.4          1.7         0.2     setosa
## 22           5.1         3.7          1.5         0.4     setosa
## 23           4.6         3.6          1.0         0.2     setosa
## 24           5.1         3.3          1.7         0.5     setosa
## 25           4.8         3.4          1.9         0.2     setosa
## 26           5.0         3.0          1.6         0.2     setosa
## 27           5.0         3.4          1.6         0.4     setosa
## 28           5.2         3.5          1.5         0.2     setosa
## 29           5.2         3.4          1.4         0.2     setosa
## 30           4.7         3.2          1.6         0.2     setosa
## 31           4.8         3.1          1.6         0.2     setosa
## 32           5.4         3.4          1.5         0.4     setosa
## 33           5.2         4.1          1.5         0.1     setosa
## 34           5.5         4.2          1.4         0.2     setosa
## 35           4.9         3.1          1.5         0.2     setosa
## 36           5.0         3.2          1.2         0.2     setosa
## 37           5.5         3.5          1.3         0.2     setosa
## 38           4.9         3.6          1.4         0.1     setosa
## 39           4.4         3.0          1.3         0.2     setosa
## 40           5.1         3.4          1.5         0.2     setosa
## 41           5.0         3.5          1.3         0.3     setosa
## 42           4.5         2.3          1.3         0.3     setosa
## 43           4.4         3.2          1.3         0.2     setosa
## 44           5.0         3.5          1.6         0.6     setosa
## 45           5.1         3.8          1.9         0.4     setosa
## 46           4.8         3.0          1.4         0.3     setosa
## 47           5.1         3.8          1.6         0.2     setosa
## 48           4.6         3.2          1.4         0.2     setosa
## 49           5.3         3.7          1.5         0.2     setosa
## 50           5.0         3.3          1.4         0.2     setosa
## 51           7.0         3.2          4.7         1.4 versicolor
## 52           6.4         3.2          4.5         1.5 versicolor
## 53           6.9         3.1          4.9         1.5 versicolor
## 54           5.5         2.3          4.0         1.3 versicolor
## 55           6.5         2.8          4.6         1.5 versicolor
## 56           5.7         2.8          4.5         1.3 versicolor
## 57           6.3         3.3          4.7         1.6 versicolor
## 58           4.9         2.4          3.3         1.0 versicolor
## 59           6.6         2.9          4.6         1.3 versicolor
## 60           5.2         2.7          3.9         1.4 versicolor
## 61           5.0         2.0          3.5         1.0 versicolor
## 62           5.9         3.0          4.2         1.5 versicolor
## 63           6.0         2.2          4.0         1.0 versicolor
## 64           6.1         2.9          4.7         1.4 versicolor
## 65           5.6         2.9          3.6         1.3 versicolor
## 66           6.7         3.1          4.4         1.4 versicolor
## 67           5.6         3.0          4.5         1.5 versicolor
## 68           5.8         2.7          4.1         1.0 versicolor
## 69           6.2         2.2          4.5         1.5 versicolor
## 70           5.6         2.5          3.9         1.1 versicolor
## 71           5.9         3.2          4.8         1.8 versicolor
## 72           6.1         2.8          4.0         1.3 versicolor
## 73           6.3         2.5          4.9         1.5 versicolor
## 74           6.1         2.8          4.7         1.2 versicolor
## 75           6.4         2.9          4.3         1.3 versicolor
## 76           6.6         3.0          4.4         1.4 versicolor
## 77           6.8         2.8          4.8         1.4 versicolor
## 78           6.7         3.0          5.0         1.7 versicolor
## 79           6.0         2.9          4.5         1.5 versicolor
## 80           5.7         2.6          3.5         1.0 versicolor
## 81           5.5         2.4          3.8         1.1 versicolor
## 82           5.5         2.4          3.7         1.0 versicolor
## 83           5.8         2.7          3.9         1.2 versicolor
## 84           6.0         2.7          5.1         1.6 versicolor
## 85           5.4         3.0          4.5         1.5 versicolor
## 86           6.0         3.4          4.5         1.6 versicolor
## 87           6.7         3.1          4.7         1.5 versicolor
## 88           6.3         2.3          4.4         1.3 versicolor
## 89           5.6         3.0          4.1         1.3 versicolor
## 90           5.5         2.5          4.0         1.3 versicolor
## 91           5.5         2.6          4.4         1.2 versicolor
## 92           6.1         3.0          4.6         1.4 versicolor
## 93           5.8         2.6          4.0         1.2 versicolor
## 94           5.0         2.3          3.3         1.0 versicolor
## 95           5.6         2.7          4.2         1.3 versicolor
## 96           5.7         3.0          4.2         1.2 versicolor
## 97           5.7         2.9          4.2         1.3 versicolor
## 98           6.2         2.9          4.3         1.3 versicolor
## 99           5.1         2.5          3.0         1.1 versicolor
## 100          5.7         2.8          4.1         1.3 versicolor
## 101          6.3         3.3          6.0         2.5  virginica
## 102          5.8         2.7          5.1         1.9  virginica
## 103          7.1         3.0          5.9         2.1  virginica
## 104          6.3         2.9          5.6         1.8  virginica
## 105          6.5         3.0          5.8         2.2  virginica
## 106          7.6         3.0          6.6         2.1  virginica
## 107          4.9         2.5          4.5         1.7  virginica
## 108          7.3         2.9          6.3         1.8  virginica
## 109          6.7         2.5          5.8         1.8  virginica
## 110          7.2         3.6          6.1         2.5  virginica
## 111          6.5         3.2          5.1         2.0  virginica
## 112          6.4         2.7          5.3         1.9  virginica
## 113          6.8         3.0          5.5         2.1  virginica
## 114          5.7         2.5          5.0         2.0  virginica
## 115          5.8         2.8          5.1         2.4  virginica
## 116          6.4         3.2          5.3         2.3  virginica
## 117          6.5         3.0          5.5         1.8  virginica
## 118          7.7         3.8          6.7         2.2  virginica
## 119          7.7         2.6          6.9         2.3  virginica
## 120          6.0         2.2          5.0         1.5  virginica
## 121          6.9         3.2          5.7         2.3  virginica
## 122          5.6         2.8          4.9         2.0  virginica
## 123          7.7         2.8          6.7         2.0  virginica
## 124          6.3         2.7          4.9         1.8  virginica
## 125          6.7         3.3          5.7         2.1  virginica
## 126          7.2         3.2          6.0         1.8  virginica
## 127          6.2         2.8          4.8         1.8  virginica
## 128          6.1         3.0          4.9         1.8  virginica
## 129          6.4         2.8          5.6         2.1  virginica
## 130          7.2         3.0          5.8         1.6  virginica
## 131          7.4         2.8          6.1         1.9  virginica
## 132          7.9         3.8          6.4         2.0  virginica
## 133          6.4         2.8          5.6         2.2  virginica
## 134          6.3         2.8          5.1         1.5  virginica
## 135          6.1         2.6          5.6         1.4  virginica
## 136          7.7         3.0          6.1         2.3  virginica
## 137          6.3         3.4          5.6         2.4  virginica
## 138          6.4         3.1          5.5         1.8  virginica
## 139          6.0         3.0          4.8         1.8  virginica
## 140          6.9         3.1          5.4         2.1  virginica
## 141          6.7         3.1          5.6         2.4  virginica
## 142          6.9         3.1          5.1         2.3  virginica
## 143          5.8         2.7          5.1         1.9  virginica
## 144          6.8         3.2          5.9         2.3  virginica
## 145          6.7         3.3          5.7         2.5  virginica
## 146          6.7         3.0          5.2         2.3  virginica
## 147          6.3         2.5          5.0         1.9  virginica
## 148          6.5         3.0          5.2         2.0  virginica
## 149          6.2         3.4          5.4         2.3  virginica
## 150          5.9         3.0          5.1         1.8  virginica
#page 24
n<-28
N<-20

#page 25
m=1973
m
## [1] 1973
n
## [1] 28
N+n
## [1] 48
#page 26
rm(m)
rm(n,N)
rm(list = ls())

#page 27
class(iris)
## [1] "data.frame"
mode(iris)
## [1] "list"
names(iris)
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"
length(iris)
## [1] 5
dim(iris)
## [1] 150   5
#page 29
serie1<-c(1.2,36,5.33,-26.5)
serie1
## [1]   1.20  36.00   5.33 -26.50
mode(serie1)
## [1] "numeric"
class(serie1)
## [1] "numeric"
c(1.2,36,5.33,-26.5)
## [1]   1.20  36.00   5.33 -26.50
(serie1<-c(1.2,36,5.33,-26.5))
## [1]   1.20  36.00   5.33 -26.50
#page 30
serie2<-c("bleu","vert","marron")
serie2
## [1] "bleu"   "vert"   "marron"
mode(serie2)
## [1] "character"
#serie2<-c(bleu,vert,marron)
serie3<-c(T,T,F,F,T)
serie3
## [1]  TRUE  TRUE FALSE FALSE  TRUE
#page 31
serie3<-c(TRUE,TRUE,FALSE,FALSE,TRUE)
serie3
## [1]  TRUE  TRUE FALSE FALSE  TRUE
mode(serie3)
## [1] "logical"
serie1[3]
## [1] 5.33
serie1[3:4]
## [1]   5.33 -26.50
#page 32
head(serie1,n=2)
## [1]  1.2 36.0
tail(serie1,n=2)
## [1]   5.33 -26.50
v<-c(2.3,3.5,6,14,12)
w<-c(3.2,5,0.7,1,3.5)

#page 33
x<-c(v,w)
x
##  [1]  2.3  3.5  6.0 14.0 12.0  3.2  5.0  0.7  1.0  3.5
y<-c(w,v)
y
##  [1]  3.2  5.0  0.7  1.0  3.5  2.3  3.5  6.0 14.0 12.0
v[c(2,5)]
## [1]  3.5 12.0
v[-c(2,3)]
## [1]  2.3 14.0 12.0
#page 34
v[v>4]
## [1]  6 14 12
w[v>4]
## [1] 0.7 1.0 3.5
(v+w)/2
## [1] 2.75 4.25 3.35 7.50 7.75
20+5*v
## [1] 31.5 37.5 50.0 90.0 80.0
z<-c(2.8,3,19.73)
z
## [1]  2.80  3.00 19.73
#page 35
v+z
## Warning in v + z: la taille d'un objet plus long n'est pas multiple de la taille
## d'un objet plus court
## [1]  5.10  6.50 25.73 16.80 15.00
length(v)
## [1] 5
length(z)
## [1] 3
s<-1:10
s
##  [1]  1  2  3  4  5  6  7  8  9 10
#page 36
s[3]<-35
s
##  [1]  1  2 35  4  5  6  7  8  9 10
s[s==1]<-25
s
##  [1] 25  2 35  4  5  6  7  8  9 10
s[s>=5]<-20
s
##  [1] 20  2 20  4 20 20 20 20 20 20
donnees<-c(1,2,3)
donnees
## [1] 1 2 3
#page 37
rep(x=donnees,times=2)
## [1] 1 2 3 1 2 3
rep(x=donnees,2)
## [1] 1 2 3 1 2 3
rep(1,50)
##  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [39] 1 1 1 1 1 1 1 1 1 1 1 1
rep("chien",4)
## [1] "chien" "chien" "chien" "chien"
#page 38
notes.Guillaume<-c(Anglais=12,Informatique=19.5,Biologie=14)
notes.Guillaume
##      Anglais Informatique     Biologie 
##         12.0         19.5         14.0
matiere<-c("Anglais","Informatique","Biologie")
matiere
## [1] "Anglais"      "Informatique" "Biologie"
note<-c(12,19.5,14)
note
## [1] 12.0 19.5 14.0
names(note)<-matiere
note
##      Anglais Informatique     Biologie 
##         12.0         19.5         14.0
names(note)<-NULL
note
## [1] 12.0 19.5 14.0
#page 39
sort(note)
## [1] 12.0 14.0 19.5
rev(sort(note))
## [1] 19.5 14.0 12.0
rev(note)
## [1] 14.0 19.5 12.0
serie4<-c(1.2,36,NA,-26.5)
serie4
## [1]   1.2  36.0    NA -26.5
#page 40
mode(serie4)
## [1] "numeric"
is.na
## function (x)  .Primitive("is.na")
is.na(serie4)
## [1] FALSE FALSE  TRUE FALSE
matrice1<-matrix(1:12,ncol=3)
matrice1
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
#page 41
matrice2<-matrix(1:12,ncol=3,byrow=TRUE)
matrice2
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
class(matrice2)
## [1] "matrix" "array"
length(matrice2)
## [1] 12
#page 42
dim(matrice2)
## [1] 4 3
matrice3<-matrix(1:12,nrow=4,ncol=4)
## Warning in matrix(1:12, nrow = 4, ncol = 4): data length differs from size of
## matrix: [12 != 4 x 4]
matrice3
##      [,1] [,2] [,3] [,4]
## [1,]    1    5    9    1
## [2,]    2    6   10    2
## [3,]    3    7   11    3
## [4,]    4    8   12    4
matrice3[3,3]
## [1] 11
#page 43
matrice3[3,]
## [1]  3  7 11  3
matrice3[,3]
## [1]  9 10 11 12
matrice3[,3,drop=FALSE]
##      [,1]
## [1,]    9
## [2,]   10
## [3,]   11
## [4,]   12
#page 44
(matrice4<-matrice3[,c(2,4)])
##      [,1] [,2]
## [1,]    5    1
## [2,]    6    2
## [3,]    7    3
## [4,]    8    4
(matrice5<-matrice3[,-1])
##      [,1] [,2] [,3]
## [1,]    5    9    1
## [2,]    6   10    2
## [3,]    7   11    3
## [4,]    8   12    4
nrow(matrice5)
## [1] 4
#page 45
ncol(matrice5)
## [1] 3
dim(matrice5)
## [1] 4 3
rbind(matrice5,c(13:15))
##      [,1] [,2] [,3]
## [1,]    5    9    1
## [2,]    6   10    2
## [3,]    7   11    3
## [4,]    8   12    4
## [5,]   13   14   15
cbind(matrice5,c(13:16))
##      [,1] [,2] [,3] [,4]
## [1,]    5    9    1   13
## [2,]    6   10    2   14
## [3,]    7   11    3   15
## [4,]    8   12    4   16
#page 46
matrice6<-matrix(1:6,ncol=3)
matrice6
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
matrice7<-matrix(1:12,ncol=4)
matrice7
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
matrice8<-matrice6 %*% matrice7
matrice8
##      [,1] [,2] [,3] [,4]
## [1,]   22   49   76  103
## [2,]   28   64  100  136
#page 47
try(matrice6 * matrice7)
## Error in matrice6 * matrice7 : tableaux de tailles inadéquates
matrice9<-matrix(7:12,ncol=3)
matrice9
##      [,1] [,2] [,3]
## [1,]    7    9   11
## [2,]    8   10   12
matrice10<-matrice6 * matrice9
matrice10
##      [,1] [,2] [,3]
## [1,]    7   27   55
## [2,]   16   40   72
matrice11<-matrice9 * matrice6

#page 48
matrice11<-matrice9 * matrice6
matrice11
##      [,1] [,2] [,3]
## [1,]    7   27   55
## [2,]   16   40   72
try(matrice12<-matrice7 %*% matrice6)
## Error in matrice7 %*% matrice6 : arguments inadéquats
#page 49
mode
## function (x) 
## {
##     if (is.expression(x)) 
##         return("expression")
##     if (is.call(x)) 
##         return(switch(deparse(x[[1L]])[1L], `(` = "(", "call"))
##     if (is.name(x)) 
##         "name"
##     else switch(tx <- typeof(x), double = , integer = "numeric", 
##         closure = , builtin = , special = "function", tx)
## }
## <bytecode: 0x7fa56231c180>
## <environment: namespace:base>
#page 50
args(matrix)
## function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) 
## NULL
#page 51
aov(Sepal.Length~Species,data=iris)
## Call:
##    aov(formula = Sepal.Length ~ Species, data = iris)
## 
## Terms:
##                  Species Residuals
## Sum of Squares  63.21213  38.95620
## Deg. of Freedom        2       147
## 
## Residual standard error: 0.5147894
## Estimated effects may be unbalanced
#jeu1<-scan()
#1.2
#36
#5.33
#

#page 52
#jeu1

#matrix(scan(),nrow=2,byrow=TRUE)
#1 3 4
#5 2 1

mat<-c(19.6,17.6,18.2,16.0)
phy<-c(19.1,17.8,18.7,16.1)

#page 53
res<-data.frame(mat,phy)
res
##    mat  phy
## 1 19.6 19.1
## 2 17.6 17.8
## 3 18.2 18.7
## 4 16.0 16.1
res2<-data.frame(mat,phy,row.names=c("Guillaume","Val\'erie","Thomas","Julie"))
res2
##            mat  phy
## Guillaume 19.6 19.1
## Val'erie  17.6 17.8
## Thomas    18.2 18.7
## Julie     16.0 16.1
#page 54
getwd()
## [1] "/Users/fbertran/Documents/GitHub/BioStatR/demo"
#setwd("C:\\Data")
#setwd("C:/Data")

#page 55
Chemin<-"/Users/fbertran/Documents/GitHub/R3ed_complements/"
Chemin
## [1] "/Users/fbertran/Documents/GitHub/R3ed_complements/"
pH<-c(1.2,3.5,11.0,7.1,8.2)

#page 56
pH
## [1]  1.2  3.5 11.0  7.1  8.2
setwd(Chemin)
save(pH,file="FichierpH.RData")
#page 55
rm(pH)
try(pH)
## Error in try(pH) : objet 'pH' introuvable
load("FichierpH.RData")
pH
## [1]  1.2  3.5 11.0  7.1  8.2
#page 57
read.table(paste(Chemin,"table1.txt",sep=""))
##     V1  V2
## 1 53.5 160
## 2 74.4 172
## 3 52.6 151
## 4 88.6 163
## 5 49.2 169
read.table("table1.txt")
##     V1  V2
## 1 53.5 160
## 2 74.4 172
## 3 52.6 151
## 4 88.6 163
## 5 49.2 169
#read.table(file.choose())

#page 58
read.table("https://fbertran.github.io/homepage/BioStatR/table1.txt")
##     V1  V2
## 1 53.5 160
## 2 74.4 172
## 3 52.6 151
## 4 88.6 163
## 5 49.2 169
table1<-read.table("table1.txt")
table1
##     V1  V2
## 1 53.5 160
## 2 74.4 172
## 3 52.6 151
## 4 88.6 163
## 5 49.2 169
table1$V1
## [1] 53.5 74.4 52.6 88.6 49.2
#page 59
table1[1,1]
## [1] 53.5
table1[c(1),c(1)]
## [1] 53.5
table1[1:2,1]
## [1] 53.5 74.4
table1[1:2,1:2]
##     V1  V2
## 1 53.5 160
## 2 74.4 172
masse<-table1$V1
taille<-table1$V2
masse
## [1] 53.5 74.4 52.6 88.6 49.2
#page 60
taille
## [1] 160 172 151 163 169
read.table("table2.txt",header=TRUE)
##   Masse Taille
## 1  53.5    160
## 2  74.4    172
## 3  52.6    151
## 4  88.6    163
## 5  49.2    169
read.table("table3.txt",dec=",")
##     V1  V2
## 1 53.5 160
## 2 74.4 172
## 3 52.6 151
## 4 88.6 163
## 5 49.2 169
read.table("table4.txt",sep=";")
##     V1  V2
## 1 53.5 160
## 2 74.4 172
## 3 52.6 151
## 4 88.6 163
## 5 49.2 169
#page 61
#write.table(table1,file=file.choose())
read.csv("table6.csv")
##   Masse Taille
## 1  53.5    160
## 2  74.4    172
## 3  52.6    151
## 4  88.6    163
## 5  49.2    169
read.csv2("table5.csv")
##   Masse Taille
## 1  53.5    160
## 2  74.4    172
## 3  52.6    151
## 4  88.6    163
## 5  49.2    169
#write.csv(table1,file=file.choose())
#write.csv2(table1,file=file.choose())

#page 63
if(!("xlsx" %in% rownames(installed.packages()))){install.packages("xlsx")}
library(xlsx)
(data<-read.xlsx("table7.xls",1))
##   Masse Taille      BMI
## 1  53.5    160 20.89844
## 2  74.4    172 25.14873
## 3  52.6    151 23.06916
## 4  88.6    163 33.34713
## 5  49.2    169 17.22629
args(read.xlsx)
## function (file, sheetIndex, sheetName = NULL, rowIndex = NULL, 
##     startRow = NULL, endRow = NULL, colIndex = NULL, as.data.frame = TRUE, 
##     header = TRUE, colClasses = NA, keepFormulas = FALSE, encoding = "unknown", 
##     password = NULL, ...) 
## NULL
#page 65
data$BMI<-data$Masse/(data$Taille/100)^2
write.xlsx(x=data,file="table10.xlsx",sheetName="FeuilleTest",row.names=FALSE)
write.xlsx(x=data,file="table10.xlsx",sheetName="AutreFeuilleTest",row.names=FALSE,append=TRUE)

#page 66
args(write.xlsx)
## function (x, file, sheetName = "Sheet1", col.names = TRUE, row.names = TRUE, 
##     append = FALSE, showNA = TRUE, password = NULL) 
## NULL
wb<-loadWorkbook("table10.xlsx")
feuilles <- getSheets(wb)
feuille <- feuilles[[1]]

#page 67
feuille <- createSheet(wb, sheetName="ajout1")
addDataFrame(x=data,sheet=feuille,row.names = FALSE, startRow = 1, startColumn = 5)
feuille2 <- createSheet(wb, sheetName="graphique")
png(filename = "matplotdata.png", width=6, height=6, units= "in", pointsize=12, res=120)
plot(data)
dev.off()
## quartz_off_screen 
##                 2
addPicture("matplotdata.png", feuille2, scale=1, startRow =2, startColumn=2)

png(filename = "matplotdata2.png", width=6, height=8, units= "in", pointsize=12, res=300)
plot(data)
dev.off()
## quartz_off_screen 
##                 2
addPicture("matplotdata2.png", feuille2, scale=.4, startRow =62, startColumn=1)
addPicture("matplotdata2.png", feuille2, scale=1, startRow =62, startColumn=14)

#page 68
saveWorkbook(wb,"table8bis.xlsx")

#if(!("RODBC" %in% rownames(installed.packages()))){install.packages("RODBC")}
#library(RODBC)
#connexion<-odbcConnectExcel()
# sqlTables(connexion)
#data<-sqlFetch(connexion,"Feuil1")
#close(connexion)
#data

#page 69
#connexion<-odbcConnectExcel(,readOnly=FALSE)
#data<-sqlFetch(connexion,"Feuil1")
#data$BMI<-data$Masse/(data$Taille/100)^2
#sqlSave(connexion,data,rownames=FALSE)
#close(connexion)

#connexion<-odbcConnectExcel(,readOnly=FALSE)
#data<-sqlFetch(connexion,"Feuil2")
#data$BMI<-data$Masse/(data$Taille/100)^2
#sqlUpdate(connexion,data,"Feuil2",index="F1")
#close(connexion)

#page 70
if(!("gdata" %in% rownames(installed.packages()))){install.packages("gdata")}
library(gdata)
## gdata: read.xls support for 'XLS' (Excel 97-2004) files ENABLED.
## 
## gdata: read.xls support for 'XLSX' (Excel 2007+) files ENABLED.
## 
## Attachement du package : 'gdata'
## L'objet suivant est masqué depuis 'package:stats':
## 
##     nobs
## L'objet suivant est masqué depuis 'package:utils':
## 
##     object.size
## L'objet suivant est masqué depuis 'package:base':
## 
##     startsWith
read.xls("table7.xls")
##   Masse Taille      BMI
## 1  53.5    160 20.89844
## 2  74.4    172 25.14873
## 3  52.6    151 23.06916
## 4  88.6    163 33.34713
## 5  49.2    169 17.22629
#Pas de donn\'ees dans la feuille 2 donc erreur lors de la lecture
#read.xls("table7.xls",sheet=2)

#page 71
#read.xls("https://fbertran.github.io/homepage/BioStatR/table7.xls",sheet=1)
if(!("XLConnect" %in% rownames(installed.packages()))){install.packages("XLConnect")}
#vignette("XLConnect")
#vignette("XLConnectImpatient")

#page 77
u<-1:10
v<-1:8
outer(u,v,"*")
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
##  [1,]    1    2    3    4    5    6    7    8
##  [2,]    2    4    6    8   10   12   14   16
##  [3,]    3    6    9   12   15   18   21   24
##  [4,]    4    8   12   16   20   24   28   32
##  [5,]    5   10   15   20   25   30   35   40
##  [6,]    6   12   18   24   30   36   42   48
##  [7,]    7   14   21   28   35   42   49   56
##  [8,]    8   16   24   32   40   48   56   64
##  [9,]    9   18   27   36   45   54   63   72
## [10,]   10   20   30   40   50   60   70   80
x<-c(NA,FALSE,TRUE)
names(x)<-as.character(x)
!x
##  <NA> FALSE  TRUE 
##    NA  TRUE FALSE
outer(x,x,"&")
##        <NA> FALSE  TRUE
## <NA>     NA FALSE    NA
## FALSE FALSE FALSE FALSE
## TRUE     NA FALSE  TRUE
#page 78
outer(x,x,"|")
##       <NA> FALSE TRUE
## <NA>    NA    NA TRUE
## FALSE   NA FALSE TRUE
## TRUE  TRUE  TRUE TRUE
outer(x,x,"xor")
##       <NA> FALSE  TRUE
## <NA>    NA    NA    NA
## FALSE   NA FALSE  TRUE
## TRUE    NA  TRUE FALSE
#page 79
#Exercice 2.1
v<-101:112
v
##  [1] 101 102 103 104 105 106 107 108 109 110 111 112
#page 80
v<-seq(101,112)
v
##  [1] 101 102 103 104 105 106 107 108 109 110 111 112
w<-rep(c(4,6,3),4)
w
##  [1] 4 6 3 4 6 3 4 6 3 4 6 3
length(w)
## [1] 12
x<-c(rep(4,8),rep(6,7),rep(3,5))
x
##  [1] 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 3 3 3 3 3
length(x)
## [1] 20
x<-rep(c(4,6,3),c(8,7,5))
x
##  [1] 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 3 3 3 3 3
#page 81
#Exercice 2.2
masse<-c(28,27.5,27,28,30.5,30,31,29.5,30,31,31,31.5,32,30,30.5)
masse
##  [1] 28.0 27.5 27.0 28.0 30.5 30.0 31.0 29.5 30.0 31.0 31.0 31.5 32.0 30.0 30.5
masse1<-c(40,39,41,37.5,43)
masse1
## [1] 40.0 39.0 41.0 37.5 43.0
nouvelle.masse<-c(rep(masse1,2),masse[6:15])
nouvelle.masse
##  [1] 40.0 39.0 41.0 37.5 43.0 40.0 39.0 41.0 37.5 43.0 30.0 31.0 29.5 30.0 31.0
## [16] 31.0 31.5 32.0 30.0 30.5
length(nouvelle.masse)
## [1] 20
#page 82
(nouvelle.masse<-c(rep(masse1,2),tail(masse,n=10)))
##  [1] 40.0 39.0 41.0 37.5 43.0 40.0 39.0 41.0 37.5 43.0 30.0 31.0 29.5 30.0 31.0
## [16] 31.0 31.5 32.0 30.0 30.5
nouvelle.masse
##  [1] 40.0 39.0 41.0 37.5 43.0 40.0 39.0 41.0 37.5 43.0 30.0 31.0 29.5 30.0 31.0
## [16] 31.0 31.5 32.0 30.0 30.5
library(xlsx)
write.xlsx(nouvelle.masse,file="Masse.xlsx")
write.xlsx(data.frame(masse=nouvelle.masse),file="Masse.xlsx")
#massedf<-data.frame(nouvelle.masse)
#library(RODBC)
#connexion<-odbcConnectExcel("Resultat.xls",readOnly = FALSE)
#sqlSave(connexion,massedf)
#close(connexion)

#page 83
#Exercice 2.3
nom<-c("Guillaume","Val\'erie","Thomas","Julie","S\'ebastien","St\'ephanie","Gr\'egory","Ambre",
       "Jean-S\'ebastien","Camille")
nom
##  [1] "Guillaume"       "Val'erie"        "Thomas"          "Julie"          
##  [5] "S'ebastien"      "St'ephanie"      "Gr'egory"        "Ambre"          
##  [9] "Jean-S'ebastien" "Camille"
age<-c(25,24,23,22,41,40,59,58,47,56)
names(age)<-nom
age
##       Guillaume        Val'erie          Thomas           Julie      S'ebastien 
##              25              24              23              22              41 
##      St'ephanie        Gr'egory           Ambre Jean-S'ebastien         Camille 
##              40              59              58              47              56
str(age)
##  Named num [1:10] 25 24 23 22 41 40 59 58 47 56
##  - attr(*, "names")= chr [1:10] "Guillaume" "Val'erie" "Thomas" "Julie" ...
masse
##  [1] 28.0 27.5 27.0 28.0 30.5 30.0 31.0 29.5 30.0 31.0 31.0 31.5 32.0 30.0 30.5
c("Guillaume"=25,"Val\'erie"=24,"Thomas"=23,"Julie"=22,"S\'ebastien"=41,
  "St\'ephanie"=40,"Gr\'egory"=59,"Ambre"=58,"Jean-S\'ebastien"=47,"Camille"=56)
##       Guillaume        Val'erie          Thomas           Julie      S'ebastien 
##              25              24              23              22              41 
##      St'ephanie        Gr'egory           Ambre Jean-S'ebastien         Camille 
##              40              59              58              47              56
#page 84
age<-data.frame(age,row.names=nom)
age
##                 age
## Guillaume        25
## Val'erie         24
## Thomas           23
## Julie            22
## S'ebastien       41
## St'ephanie       40
## Gr'egory         59
## Ambre            58
## Jean-S'ebastien  47
## Camille          56
masse<-c(66.5,50.5,67.5,52,83,65,79,64,81,53)
names(masse)<-nom
masse
##       Guillaume        Val'erie          Thomas           Julie      S'ebastien 
##            66.5            50.5            67.5            52.0            83.0 
##      St'ephanie        Gr'egory           Ambre Jean-S'ebastien         Camille 
##            65.0            79.0            64.0            81.0            53.0
#page 85
masse<-data.frame(masse,row.names=nom)
masse
##                 masse
## Guillaume        66.5
## Val'erie         50.5
## Thomas           67.5
## Julie            52.0
## S'ebastien       83.0
## St'ephanie       65.0
## Gr'egory         79.0
## Ambre            64.0
## Jean-S'ebastien  81.0
## Camille          53.0
taille<-c(1.86,1.62,1.72,1.67,1.98,1.77,1.83,1.68,1.92,1.71)
names(taille)<-nom
taille
##       Guillaume        Val'erie          Thomas           Julie      S'ebastien 
##            1.86            1.62            1.72            1.67            1.98 
##      St'ephanie        Gr'egory           Ambre Jean-S'ebastien         Camille 
##            1.77            1.83            1.68            1.92            1.71
taille<-data.frame(taille,row.names=nom)
taille
##                 taille
## Guillaume         1.86
## Val'erie          1.62
## Thomas            1.72
## Julie             1.67
## S'ebastien        1.98
## St'ephanie        1.77
## Gr'egory          1.83
## Ambre             1.68
## Jean-S'ebastien   1.92
## Camille           1.71
#page 86
masse.lourde<-masse[masse>80]
masse.lourde
## [1] 83 81
masse<-data.frame(masse,row.names=nom)
masse.lourde<-masse[masse>80]
masse.lourde
## [1] 83 81
str(masse.lourde)
##  num [1:2] 83 81
#page 87
masse.lourde<-masse[masse>80,,drop=FALSE]
masse.lourde
##                 masse
## S'ebastien         83
## Jean-S'ebastien    81
masse.lourde<-masse[masse>80]
taille.masse.lourde<-taille[masse>=80]
taille.masse.lourde
## [1] 1.98 1.92
taille.masse.lourde<-taille[masse>=80,,drop=FALSE]
taille.masse.lourde
##                 taille
## S'ebastien        1.98
## Jean-S'ebastien   1.92
#page 88
taille.vieux.masse.lourde<-taille[masse>=80 & age>=30]
taille.vieux.masse.lourde
## [1] 1.98 1.92
taille.vieux.masse.lourde<-taille[masse>=80 & age>=30,,drop=FALSE]
taille.vieux.masse.lourde
##                 taille
## S'ebastien        1.98
## Jean-S'ebastien   1.92
ensemble<-cbind(age,masse,taille)
ensemble
##                 age masse taille
## Guillaume        25  66.5   1.86
## Val'erie         24  50.5   1.62
## Thomas           23  67.5   1.72
## Julie            22  52.0   1.67
## S'ebastien       41  83.0   1.98
## St'ephanie       40  65.0   1.77
## Gr'egory         59  79.0   1.83
## Ambre            58  64.0   1.68
## Jean-S'ebastien  47  81.0   1.92
## Camille          56  53.0   1.71
#page 89
suite<-1:12
suite
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
suite>6
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
suite<6
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
!(suite>=6)
##  [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
suite==6
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
#page 90
suite<=6 & suite>=6
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
suite<=8 && suite>=4
## Warning in suite <= 8 && suite >= 4: ‘length(x) = 12 > 1’ dans la conversion
## automatique vers ‘logical(1)’

## Warning in suite <= 8 && suite >= 4: ‘length(x) = 12 > 1’ dans la conversion
## automatique vers ‘logical(1)’
## [1] FALSE
suite<=4 | suite>=8
##  [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
suite<=4||suite>=8
## Warning in suite <= 4 || suite >= 8: ‘length(x) = 12 > 1’ dans la conversion
## automatique vers ‘logical(1)’
## [1] TRUE