IMPORTANT : mardi 20 avril et jeudi 22 avril entre 16h et 19h, une maintenance des équipements réseau de l'UGA entrainera une coupure de la plate-forme gitlab. Des perturbations sont également possibles entre ces deux dates.

Commit 1440ca9a authored by Laurence Viry's avatar Laurence Viry

ajout études

parent 089e687d
Pipeline #11234 passed with stages
in 54 seconds
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Ce notebook est issu du document fourni par H. Commenges lors d'une formation pour ATMO en avril 2017*\n",
"\n",
"## Création, manipulation d'objets R et de structures de données\n",
"\n",
"### Création d'objets\n",
"\n",
"#### Assignation\n",
"\n",
"* Opérateur d'assignation \"**<-**\"\n",
"* \"**<-**\" et \"**=**\" sont équivalents\n",
"\n",
"L'opérateur \"**<-**\" est conseillé"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"1+1\n",
"Var1Somme <- 1+1\n",
"Var1Somme\n",
"Var2Somme <- 7+6\n",
"Var2Somme\n",
"VarSommeGlob <- Var1Somme + Var2Somme\n",
"VarSommeGlob\n",
"# Essayer d'autres opérations ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Var2Somme <- 8\n",
"Var2Somme"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Référencement\n",
"* Si le nom n'existe pas, l'assignation crée de nouvelle référence.\n",
"* Si le nom existe, l'assignation écrase la référence existante.\n",
"\n",
"### Typage dynamique\n",
"\n",
"#### Typage à la volée"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#\n",
"estNum <- 1.67\n",
"estInt <- 45L\n",
"estAlphaNum <- \"taux\"\n",
"estLogique <- TRUE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Se renseigner et changer de type"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class(estNum)\n",
"mode(estNum)\n",
"is.numeric(estNum)\n",
"#\n",
"class(estInt)\n",
"mode(estInt)\n",
"is.numeric(estInt)\n",
"#\n",
"class(estAlphaNum)\n",
"is.numeric(estAlphaNum)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Calcul terme à terme\n",
"\n",
"* Les opérations sont effectués vectoriellement terme à terme\n",
"* il y a des opérateurs spécifiques pour le calcul matriciel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vec1 <- c(3,8,-8,10,11)\n",
"vec1\n",
"vec2 <- rep(4,5)\n",
"vec2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Opération\n",
"vec1 + vec2\n",
"vec1*6\n",
"vec1*vec2\n",
"vec1%*%vec2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Manipulation de structure de données\n",
"\n",
"### Les Vecteurs\n",
"Suite ordonnée de valeurs **de même type**(numérique, alphanumérique, logique) en *une dimension*\n",
"\n",
"* Créer l'objet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c(6,7,1,2,9)\n",
"vecTaille <- c(10, 40, 20, 17, 12)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Se renseigner sur l'objet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str(vecTaille)\n",
"class(vecTaille)\n",
"length(vecTaille)\n",
"names(vecTaille)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Désigner des valeurs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vecTaille[3]\n",
"vecTaille[c(1,3)]\n",
"vecTaille[1:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* Utiliser des fonctions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sum(vecTaille[3])\n",
"mean(vecTaille)\n",
"summary(vecTaille)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Les matrices\n",
"\n",
"* Suite de valeurs de **même type** (numérique, alphanumérique, logique) en **n dimensions**\n",
"* *Classe* : **matrix**(2 dimensions) ou **array** (n dimensions)\n",
"\n",
"#### Créer l'objet:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"seq(0,6,2)\n",
"mat <- matrix(c(c(2.3,4,7.8,9.2),rep(2,4),seq(0,6,2)),nrow=4,ncol=3)\n",
"mat"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Se renseigner sur l'objet\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str(mat)\n",
"class(mat)\n",
"dim(mat)\n",
"rownames(mat)\n",
"colnames(mat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Désigner les valeurs\n",
"Les valeurs sont stockées colonne par colonne, on peut modifier en spécifiant l'option **byrow=TRUE**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mat[4]\n",
"mat[1,2]\n",
"mat[1,] # ligne 1\n",
"mat[,1] # colonne 1\n",
"matrow <- matrix(c(c(2.3,4,7.8,9.2),rep(2,4),seq(0,6,2)),nrow=4,ncol=3,byrow=TRUE)\n",
"matrow\n",
"matrow[4]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Faire des opérations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sum(mat)\n",
"mean(mat)\n",
"mean(mat[,1])\n",
"summary(mat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tableau\n",
"\n",
"* Ensemble de **vecteurs** de **même taille** mais pouvant être **de type différent**.\n",
"\n",
"* Classe : **data.frame**\n",
"\n",
"#### Créer l'objet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tabCastors1 <- data.frame(NOM=c(\"Riri\",\"Fifi\",\"Loulou\"),\n",
" TAILLE=c(1.43,1.78,1.03),\n",
" POIDS=c(50,87,45))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tabCastors1\n",
"# Nom des lignes\n",
"row.names(tabCastors1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tabCastors2 <- data.frame(NOM=c(\"Riri\",\"Fifi\",\"Loulou\"),\n",
" TAILLE=c(1.43,1.78,1.03),\n",
" POIDS=c(50,87,45),row.names=\"NOM\")\n",
"row.names(tabCastors2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Se renseigner sur l'objet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str(tabCastors2)\n",
"class(tabCastors2)\n",
"dim(tabCastors2)\n",
"row.names(tabCastors2)\n",
"colnames(tabCastors2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Faire des opérations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"summary(tabCastors1)\n",
"summary(tabCastors2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# sum(tabCastors1)\n",
"sum(tabCastors2)\n",
"sum(tabCastors1$TAILLE)\n",
"mean(tabCastors2$TAILLE)\n",
"summary(tabCastors2)\n",
"summary(tabCastors2$TAILLE)\n",
"attributes(tabCastors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Liste\n",
"\n",
"* Ensemble d'objets **de tout type** et **de toute taille**\n",
"\n",
"* Classe : **list**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Créer un objet"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [],
"source": [
"riri <- list(NOM=\"Riri\",TAILLE=data.frame(DATE=c(1951,1952,1953),TAILLE=c(1.43,1.48,1.54)),\n",
" COORDS=c(33.94,-117.51))\n",
"fifi <- list(NOM=\"Fifi\",TAILLE=data.frame(DATE=c(1951,1952,1953),TAILLE=c(1.78,1.80,1.87)),\n",
" COORDS=c(33.21,-117.64))\n",
"loulou <- list(NOM=\"Loulou\",TAILLE=data.frame(DATE=c(1951,1952,1953),TAILLE=c(1.04,1.10,1.20)),\n",
" COORDS=c(33.83,-117.25))\n",
"listCastors <-list(RIRI=riri,FIFI=fifi,LOULOU=loulou)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Se renseigner sur l'objet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str(riri)\n",
"class(riri)\n",
"str(listCastors)\n",
"class(listCastors)\n",
"length(listCastors)\n",
"names(riri)\n",
"names(listCastors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Désigner des valeurs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"riri[1]\n",
"class(riri[1])\n",
"riri[[1]]\n",
"class(riri[[1]])\n",
"riri$NOM"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
"<thead><tr><th scope=col>DATE</th><th scope=col>TAILLE</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td>1951</td><td>1.43</td></tr>\n",
"\t<tr><td>1952</td><td>1.48</td></tr>\n",
"\t<tr><td>1953</td><td>1.54</td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\begin{tabular}{r|ll}\n",
" DATE & TAILLE\\\\\n",
"\\hline\n",
"\t 1951 & 1.43\\\\\n",
"\t 1952 & 1.48\\\\\n",
"\t 1953 & 1.54\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"DATE | TAILLE | \n",
"|---|---|---|\n",
"| 1951 | 1.43 | \n",
"| 1952 | 1.48 | \n",
"| 1953 | 1.54 | \n",
"\n",
"\n"
],
"text/plain": [
" DATE TAILLE\n",
"1 1951 1.43 \n",
"2 1952 1.48 \n",
"3 1953 1.54 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"'data.frame'"
],
"text/latex": [
"'data.frame'"
],
"text/markdown": [
"'data.frame'"
],
"text/plain": [
"[1] \"data.frame\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<strong>$TAILLE</strong> = <table>\n",
"<thead><tr><th scope=col>DATE</th><th scope=col>TAILLE</th></tr></thead>\n",
"<tbody>\n",
"\t<tr><td>1951</td><td>1.43</td></tr>\n",
"\t<tr><td>1952</td><td>1.48</td></tr>\n",
"\t<tr><td>1953</td><td>1.54</td></tr>\n",
"</tbody>\n",
"</table>\n"
],
"text/latex": [
"\\textbf{\\$TAILLE} = \\begin{tabular}{r|ll}\n",
" DATE & TAILLE\\\\\n",
"\\hline\n",
"\t 1951 & 1.43\\\\\n",
"\t 1952 & 1.48\\\\\n",
"\t 1953 & 1.54\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"**$TAILLE** = \n",
"DATE | TAILLE | \n",
"|---|---|---|\n",
"| 1951 | 1.43 | \n",
"| 1952 | 1.48 | \n",
"| 1953 | 1.54 | \n",
"\n",
"\n"
],
"text/plain": [
"$TAILLE\n",
" DATE TAILLE\n",
"1 1951 1.43\n",
"2 1952 1.48\n",
"3 1953 1.54\n"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"'list'"
],
"text/latex": [
"'list'"
],
"text/markdown": [
"'list'"
],
"text/plain": [
"[1] \"list\""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#\n",
"listCastors[[1]][[2]]\n",
"class(listCastors[[1]][[2]])\n",
"listCastors[[1]][2]\n",
"class(listCastors[[1]][2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Faire des opérations\n",
"\n",
"Il faut appliquer des fonctions sur un ensemble d'éléments de la liste (**lapply**,...)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "ir"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
......@@ -621,7 +621,7 @@
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.4.1"
"version": "3.4.3"
}
},
"nbformat": 4,
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},