- L'opérateur d'accès
[]
est associé à deux paramètres/indices séparés par une virgule.
- Sélection d'une valeur
> lenses[2,2]
[1] myope
Levels: hypermetrope myope
> as.character(lenses[2,2])
[1] "myope"
Par défaut la valeur retournée est un factor car elle est issue d'une variable de ce mode (type), pour obtenir un scalaire, il faut faire un transtypage.
- Sélection d'une ligne (retourne un data.frame)
> lenses[21,]
Age Defaut Astygmate Larmes Classe
21 presbyopic hypermetrope true reduce ncl
- Sélection d'une colonne (retourne un factor)
> lenses[,3]
[1] true true false false true true false false true true false false
[13] true true false false true true false false true true false false
Levels: false true
> lenses[,"Larmes"]
[1] reduce normal reduce normal reduce normal reduce normal reduce normal
[11] reduce normal reduce normal reduce normal reduce normal reduce normal
[21] reduce normal reduce normal
Levels: normal reduce
> lenses[["Larmes"]]
[1] reduce normal reduce normal reduce normal reduce normal reduce normal
[11] reduce normal reduce normal reduce normal reduce normal reduce normal
[21] reduce normal reduce normal
Levels: normal reduce
> lenses$Larmes
[1] reduce normal reduce normal reduce normal reduce normal reduce normal
[11] reduce normal reduce normal reduce normal reduce normal reduce normal
[21] reduce normal reduce normal
Levels: normal reduce
- Sélectionner les lignes 1 à 10 (retourne un data.frame) :
> lenses[1:10, ]
Age Defaut Astygmate Larmes Classe
1 young myope true reduce ncl
2 young myope true normal scl
3 young myope false reduce ncl
4 young myope false normal hcl
5 young hypermetrope true reduce ncl
6 young hypermetrope true normal scl
7 young hypermetrope false reduce ncl
8 young hypermetrope false normal hcl
9 pre-presbyopic myope true reduce ncl
10 pre-presbyopic myope true normal scl
- Sélectionner les colonnes Larmes et Classe (retourne un data.frame)
> lenses[c("Larmes", "Classe")]
Larmes Classe
1 reduce ncl
2 normal scl
3 reduce ncl
4 normal hcl
5 reduce ncl
6 normal scl
7 reduce ncl
8 normal hcl
9 reduce ncl
10 normal scl
11 reduce ncl
12 normal hcl
13 reduce ncl
14 normal scl
15 reduce ncl
16 normal ncl
17 reduce ncl
18 normal ncl
19 reduce ncl
20 normal hcl
21 reduce ncl
22 normal scl
23 reduce ncl
24 normal ncl
- Sélectionner la classe des lignes impaires :
> lenses[ seq(1,24,by=2), "Classe"]
[1] ncl ncl ncl ncl ncl ncl ncl ncl ncl ncl ncl ncl
Levels: hcl ncl scl
> lenses[ 2*(1:12), "Classe"]
[1] scl hcl scl hcl scl hcl scl ncl ncl hcl scl ncl
Levels: hcl ncl scl
> lenses[2*(1:12),]$Classe
[1] scl hcl scl hcl scl hcl scl ncl ncl hcl scl ncl
Levels: hcl ncl scl
- Sélectionner les colonnes Age et Astygmate :
> lenses[,c("Age","Astygmate")]
Age Astygmate
1 young true
2 young true
3 young false
4 young false
5 young true
6 young true
7 young false
8 young false
9 pre-presbyopic true
10 pre-presbyopic true
11 pre-presbyopic false
12 pre-presbyopic false
13 pre-presbyopic true
14 pre-presbyopic true
15 pre-presbyopic false
16 pre-presbyopic false
17 presbyopic true
18 presbyopic true
19 presbyopic false
20 presbyopic false
21 presbyopic true
22 presbyopic true
23 presbyopic false
24 presbyopic false
- Vérifier si les Larmes sont normales :
> lenses[,"Larmes"]=="normal"
[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
[13] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
- Sélectionner les données associées aux larmes normales
- Sélectionner les données associées aux lentilles souples ou rigides