#)## == égalité
## != différent
## < strictement inférieur > strictement supérieur
## <= inférieur ou égal >= supérieur ou égal
## is.na(x) valeur manquante
## & ET logique
## | OU logique
## ! NON logiquenote <- c(14,12,17,10) # y est un vecteur
note## [1] 14 12 17 10
matiere <- c("maths","histoire","anglais","philo") # y est un vecteur
matiere## [1] "maths" "histoire" "anglais" "philo"
classement <- c(8,11,3,15) # x est un vecteur
classement## [1] 8 11 3 15
data.frame(note,matiere,classement)## note matiere classement
## 1 14 maths 8
## 2 12 histoire 11
## 3 17 anglais 3
## 4 10 philo 15
Orange # un dataframe de base dans R## Tree age circumference
## 1 1 118 30
## 2 1 484 58
## 3 1 664 87
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
## 7 1 1582 145
## 8 2 118 33
## 9 2 484 69
## 10 2 664 111
## 11 2 1004 156
## 12 2 1231 172
## 13 2 1372 203
## 14 2 1582 203
## 15 3 118 30
## 16 3 484 51
## 17 3 664 75
## 18 3 1004 108
## 19 3 1231 115
## 20 3 1372 139
## 21 3 1582 140
## 22 4 118 32
## 23 4 484 62
## 24 4 664 112
## 25 4 1004 167
## 26 4 1231 179
## 27 4 1372 209
## 28 4 1582 214
## 29 5 118 30
## 30 5 484 49
## 31 5 664 81
## 32 5 1004 125
## 33 5 1231 142
## 34 5 1372 174
## 35 5 1582 177
dim(Orange) # nombre de lignes et nombre de colonnes## [1] 35 3
nrow(Orange) # nombre de lignes## [1] 35
ncol(Orange) # nombre de colonnes## [1] 3
names(Orange) # nom des colonnes (nom des variables)## [1] "Tree" "age" "circumference"
head(Orange) # affiche les 6 premières lignes du tableau## Tree age circumference
## 1 1 118 30
## 2 1 484 58
## 3 1 664 87
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
tail(Orange) # affiche les 6 dernières lignes du tableau## Tree age circumference
## 30 5 484 49
## 31 5 664 81
## 32 5 1004 125
## 33 5 1231 142
## 34 5 1372 174
## 35 5 1582 177
Exemples
Orange[1,] # première ligne## Tree age circumference
## 1 1 118 30
Orange[,2] # deuxième colonne## [1] 118 484 664 1004 1231 1372 1582 118 484 664 1004 1231 1372 1582 118
## [16] 484 664 1004 1231 1372 1582 118 484 664 1004 1231 1372 1582 118 484
## [31] 664 1004 1231 1372 1582
Orange[,ncol(Orange)] # dernière colonne## [1] 30 58 87 115 120 142 145 33 69 111 156 172 203 203 30 51 75 108 115
## [20] 139 140 32 62 112 167 179 209 214 30 49 81 125 142 174 177
Orange[Orange$Tree == 1,] # données où la variable 'Tree' prend la valeur '1'## Tree age circumference
## 1 1 118 30
## 2 1 484 58
## 3 1 664 87
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
## 7 1 1582 145
Orange[Orange$Tree == 1 & Orange$circumference > 100,]## Tree age circumference
## 4 1 1004 115
## 5 1 1231 120
## 6 1 1372 142
## 7 1 1582 145
Dans le dataframe ‘x’, retirer les lignes comportant la valeur ‘0’.
x1 <- c(0,2,1)
x2 <- c(2,3,0)
x3 <- c(5,8,7)
x <- data.frame(x1,x2,x3)
x## x1 x2 x3
## 1 0 2 5
## 2 2 3 8
## 3 1 0 7
data <- c(1,2,2,3,1,2,3,3,1,2,3,3,1) # classe: numeric
data## [1] 1 2 2 3 1 2 3 3 1 2 3 3 1
data2 <- as.character(data) # classe: character
data2## [1] "1" "2" "2" "3" "1" "2" "3" "3" "1" "2" "3" "3" "1"
as.numeric(data2) # classe: numeric## [1] 1 2 2 3 1 2 3 3 1 2 3 3 1
factor(data) # classe: factor## [1] 1 2 2 3 1 2 3 3 1 2 3 3 1
## Levels: 1 2 3
ifelserm(list=ls())
x1 <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
x2 <- c(129, 178, 140, 186, 191, 104, 150, 183, 151, 142)
x3 <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
data <- data.frame(x1, x2, x3)
data## x1 x2 x3
## 1 1 129 A
## 2 3 178 B
## 3 5 140 C
## 4 7 186 D
## 5 9 191 E
## 6 11 104 F
## 7 13 150 G
## 8 15 183 H
## 9 17 151 I
## 10 19 142 J
On veut créer une variable qui prend la valeur ‘1’ si x2 > 150, ‘0’ sinon.
Option 1
# on ajoute directement la variable dans le tableau
data$x4 <- ifelse(data$x2 > 150, 1, 0)
data## x1 x2 x3 x4
## 1 1 129 A 0
## 2 3 178 B 1
## 3 5 140 C 0
## 4 7 186 D 1
## 5 9 191 E 1
## 6 11 104 F 0
## 7 13 150 G 0
## 8 15 183 H 1
## 9 17 151 I 1
## 10 19 142 J 0
Option 2
# on crée d'abord la nouvelle variable (objet) puis on l'ajoute au tableau
x5 <- ifelse(data$x2 > 150, 1 , 0)
data2 <- cbind(data, x5)
data2## x1 x2 x3 x4 x5
## 1 1 129 A 0 0
## 2 3 178 B 1 1
## 3 5 140 C 0 0
## 4 7 186 D 1 1
## 5 9 191 E 1 1
## 6 11 104 F 0 0
## 7 13 150 G 0 0
## 8 15 183 H 1 1
## 9 17 151 I 1 1
## 10 19 142 J 0 0
rm(list=ls())
x1 <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
x2 <- c(129, 178, 140, 186, 191, 104, 150, 183, 151, 142)
x3 <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
data <- data.frame(x1, x2, x3)
data## x1 x2 x3
## 1 1 129 A
## 2 3 178 B
## 3 5 140 C
## 4 7 186 D
## 5 9 191 E
## 6 11 104 F
## 7 13 150 G
## 8 15 183 H
## 9 17 151 I
## 10 19 142 J
On veut créer une variable qui prend la valeur ‘1’ si x2 > 150, ‘0’ sinon.
Option 1
# on crée une nouvelle variable qui ne comporte que des '0'
x4 <- rep(0, nrow(data))
data.frame(data, x4)## x1 x2 x3 x4
## 1 1 129 A 0
## 2 3 178 B 0
## 3 5 140 C 0
## 4 7 186 D 0
## 5 9 191 E 0
## 6 11 104 F 0
## 7 13 150 G 0
## 8 15 183 H 0
## 9 17 151 I 0
## 10 19 142 J 0
# on réalise la boucle
for (i in 1:nrow(data)) {
x4[i] <- ifelse(data$x2[i] > 150, 1, 0)
}
data.frame(data, x4)## x1 x2 x3 x4
## 1 1 129 A 0
## 2 3 178 B 1
## 3 5 140 C 0
## 4 7 186 D 1
## 5 9 191 E 1
## 6 11 104 F 0
## 7 13 150 G 0
## 8 15 183 H 1
## 9 17 151 I 1
## 10 19 142 J 0
Option 2
for (i in 1:nrow(data)) {
if(data$x2[i] > 150) {x4[i] <- 1}
else {x4[i] <- 0}
}
data.frame(data, x4)## x1 x2 x3 x4
## 1 1 129 A 0
## 2 3 178 B 1
## 3 5 140 C 0
## 4 7 186 D 1
## 5 9 191 E 1
## 6 11 104 F 0
## 7 13 150 G 0
## 8 15 183 H 1
## 9 17 151 I 1
## 10 19 142 J 0
strsplit : séparer des caractèresx <- "Split the words in a sentence."
strsplit(x, " ")## [[1]]
## [1] "Split" "the" "words" "in" "a" "sentence."
strsplit(x, "")## [[1]]
## [1] "S" "p" "l" "i" "t" " " "t" "h" "e" " " "w" "o" "r" "d" "s" " " "i" "n" " "
## [20] "a" " " "s" "e" "n" "t" "e" "n" "c" "e" "."
dates <- c("1999-05-23", "2001-12-30", "2004-12-17")
strsplit(dates, "-")## [[1]]
## [1] "1999" "05" "23"
##
## [[2]]
## [1] "2001" "12" "30"
##
## [[3]]
## [1] "2004" "12" "17"
v <- c('a','b','c','e')
'b' %in% v## [1] TRUE
v %in% 'b'## [1] FALSE TRUE FALSE FALSE
Le vecteur ‘x’ comporte 4 nombres. Créer un vecteur qui prend la valeur ‘TRUE’ si le nombre comporte le chiffre 1, ‘FALSE’ sinon.
x <- c(145,19,237,612)# histogramme
hist(Orange$circumference)# nuage de points
plot(Orange$age,Orange$circumference) ***