Vous êtes sur la page 1sur 4

Logiciel R

Fonctions :
• Suite de nombres x:y
Suite <- 1:5
12345

• Séquence de nombre seq(from=a,to=b,by=k)


Sequence <- seq(from=1,to=9,by=2)
13579

• Vecteur créé c(x,y,z,…)


Serie <- c(1.2,1.8,2.9,7.8) Serie2 <- c(« bleu », « vert ») Serie3<-c(TRUE, FALSE)
1.2 1.8 2.9 7.8 « bleu » « vert » TRUE FALSE

• Donnée manquante NA
S <- c(1,NA,2)
1 NA 2

• Mode attribut mode(…)


Mode(Serie)
« numeric »

• Longueur attribut length(…)


Length(Serie)
4

• Elément vecteur …[…]


Serie[1] Serie(c[1,3])
1.2 1.2 1.8 2.9

• Répéter données rep(x=…,times=…)


Rep(x=Serie2,times=2)
« bleu » « vert » « bleu » « vert »

• Nommer composante vecteur


Note.Jean <- c(Anglais=12, Info=18)
Anglais Info
12 18
• Trier données vecteur sort(…) ou rev(sort(…))
Sort(note) rev(sort(note))
12 14 17 19 19 17 14 12

• Négation !...
!Logic1

• Conjonction …&…
Logic1 & Logic2

• Egalité … && …
Logic1 && Logic2

• Matrice créée colonne par colonne matrix(x:y,ncol=k)


Matrice1 <- matrix(1:6,ncol=3)
1 3 5
2 4 6

• Matrice créée ligne par ligne matrix(x:y,ncol=k,byrow=TRUE)


Matrice2 <- matrix(1:6,ncol=3,byrow=TRUE)
1 2 3
4 5 6

• Elément matrice nom-matrice[i,j]


Matrice2[3,3] Matrice2[,3] Matrice2[2,]
6 3 6 4 5 6

• Nombre de lignes matrice nrow(nom_matrice)


Nrow(Matrice2)
2

• Nombre de colonnes matrice ncol(nom_matrice)


Ncol(Matrice2)
3

• Ajouter lignes/colonnes matrice rbind(nom_matrice, valeur ajoutée) ou cbind


Rbind(Matrice2,c(1,2,3)) cbind(Matrice2,c(1,2))
1 2 3 1 2 3 1
4 5 6 4 5 6 2
1 2 3

• Multiplier 2 matrices … %% …
• Transposée matrice t(nom_matrice)

• Déterminant matrice det(nom_matrice)

• Inverse matrice solve(nom_matrice)

• Diagonaliser la matrice eigne(nom_matrice)

• Boucle for for(k in x :y)


A <- 0
For (i in 1 :50) {
A<-A+1 }
A
50

• Convertir vecteur en indice seq_along(x :y)


Seq_along(10:20)
1 2 3 4 5 6 7 8 9 10 11

• Liste (différentes natures) list(…)


list(a=1,b= « test »,c=TRUE)
$a $b $c
1 « test » TRUE

• Boucle while while (condition)


A
While (A<=50) {
A<-A+1 }
A
51

• Condition if if(condition)
If (a==1) { If …
Cat(« a=1 ») }
a=1 Else…

• Condition ifelse ifelse(condition, valeur si TRUE, valeur si FALSE)


Ifelse(1 :10==5, « egal à 5 », « pas égal à 5 »)

• Tirage aléatoire sample(x=vecteur, size=nb prél, replace=FALSE, prob=NULL)


Sample (c(0,0,1),1))
1
• Répliquer
Replicate(100, sample(1 :6,1))

• Fonction function(a,b) {return (a+b)}


Fun1<-function(a=2,b=2) {return (a+b)}
Fun1
4

Vous aimerez peut-être aussi