Commit 010eb5ea authored by Laurence Viry's avatar Laurence Viry
Browse files

mise a jour IntroR

parent 8b722593
Pipeline #9262 passed with stages
in 23 seconds
No preview for this file type
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -244,232 +244,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# R comme calculatrice\n",
"2+3\n",
"3-2\n",
"2^3\n",
"13 %% 3\n",
"13 %/% 3\n",
"log(7) #fonction prédéfinie de R\n",
"runif(4)\n",
"ls() # fournit le nom des objets de l'espace de travail"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Programmation sous R - Espace de travail\n",
"\n",
"Un concept de base en programmation s'appelle une **variable**.\n",
"\n",
"Une variable vous permet de **stocker une valeur** (par exemple 4) ou **un objet** (par exemple une description de fonction) à l’aide de l’*opérateur d’assignation* \n",
"\n",
" opérateur d'assignation ** \" <- \"** dans R. \n",
"\n",
"Vous pouvez ensuite utiliser le nom de cette variable pour accéder facilement à la valeur ou à l'objet stocké dans cette variable.\n",
"\n",
"On peut retrouver l'ensemble des objets stockés avec la fonction **ls()** dans la console ou dans l'onglet **Environment** de RStudio.\n",
"\n",
"On essaie et on observe...\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# R comme langage interprete - espace de travail\n",
"ls()\n",
"n <- 20\n",
"n\n",
"a <- runif(n)\n",
"a\n",
"b <- 10\n",
"nb <- n + b\n",
"nb\n",
"# Espace de travail\n",
"ls()\n",
"rm(n)\n",
"# Espace de travail\n",
"ls()\n",
"# Nettoyer l'espace de travail\n",
"rm(list=ls())\n",
"ls()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Quelques statistiques"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"ls()\n",
"a <- 4\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# R comme langage interprete - statistiques\n",
"n <-20\n",
"a <- runif(n)\n",
"mean(a)\n",
"sd(a)\n",
"summary(a)\n",
"barplot(a)\n",
"b <- rnorm(20,mean=0,sd=1)\n",
"hist(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Les distributions du package de base\n",
"help(Distributions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Espace de travail (workspace)\n",
"\n",
"L'espace de travail est l'ensemble des **objets** stockés, consultable:\n",
"\n",
"* Dans la console par la fonction ** ls() **.\n",
"\n",
"* Dans l'onglet **\"Environment\"** de RStudio."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Espace de travail (workspace)\n",
"ls()\n",
"\n",
"a <- 7\n",
"\n",
"ls()\n",
"\n",
"myfunc <- function() {y <- 1; ls()}\n",
"myfunc() \n",
"\n",
"# Effacer des objets \n",
"rm(a)\n",
"rm(myfunc)\n",
"ls()\n",
"\n",
"cmat1 <-matrix(1:20,ncol=5)\n",
"cmat2 <-matrix(1:20,ncol=4)\n",
"ls()\n",
"\n",
"# Effacer tout l'espace de travail\n",
"rm(list = ls())\n",
"ls()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Pour en savoir plus: [Working Directories and Workspaces](https://support.rstudio.com/hc/en-us/articles/200711843-Working-Directories-and-Workspaces)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Les types de base en R\n",
"\n",
"R utilise différents types de données dont les principaux sont:\n",
"\n",
" * Un objet vide noté: NULL\n",
" * Les *valeurs réelles* comme 4.5 sont appelées **numériques**.\n",
" * Les *nombres naturels* comme 4 sont appelés *integer*. Les nombres entiers sont aussi **numériques**.\n",
" * Les *valeurs booléennes* (TRUE ou FALSE) sont appelées **logiques**.\n",
" * Les *chaînes de caractères* comme \"nom\" sont appelées **character**.\n",
" \n",
"Les guillemets de part et d'autre d'un texte ( \"texte\") indique que c'est du type **character**\n",
"\n",
"Pour connaître le mode d'un objet **x** de R, il suffit d'exécuter la fonction **mode(x)**\n",
"\n",
"* La fonction **typeof()** permet d'obtenir une description plus précise de la représentation interne d'un objet.\n",
"\n",
"* R utilise un mécanisme de fonction générique simple, le choix de la méthode utilisée dépend de la classe du premier argument de la fonction générique (ex: plot, print,...).\n",
"\n",
"Pour connaître la classe d'une variable, on utilise la fonction **class()**"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Variables of differents types\n",
"my_numeric <- 42\n",
"\n",
"my_character <- \"42\"\n",
"my_logical <- FALSE \n",
"my_integer <- 5L"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"' mode et classe my_numeric'"
"5"
],
"text/latex": [
"' mode et classe my\\_numeric'"
"5"
],
"text/markdown": [
"' mode et classe my_numeric'"
"5"
],
"text/plain": [
"[1] \" mode et classe my_numeric\""
"[1] 5"
]
},
"metadata": {},
......@@ -478,16 +268,16 @@
{
"data": {
"text/html": [
"'numeric'"
"1"
],
"text/latex": [
"'numeric'"
"1"
],
"text/markdown": [
"'numeric'"
"1"
],
"text/plain": [
"[1] \"numeric\""
"[1] 1"
]
},
"metadata": {},
......@@ -496,16 +286,16 @@
{
"data": {
"text/html": [
"'double'"
"8"
],
"text/latex": [
"'double'"
"8"
],
"text/markdown": [
"'double'"
"8"
],
"text/plain": [
"[1] \"double\""
"[1] 8"
]
},
"metadata": {},
......@@ -514,68 +304,34 @@
{
"data": {
"text/html": [
"'numeric'"
"1"
],
"text/latex": [
"'numeric'"
"1"
],
"text/markdown": [
"'numeric'"
"1"
],
"text/plain": [
"[1] \"numeric\""
"[1] 1"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Class de my_numeric\n",
"\" mode et classe my_numeric\"\n",
"mode(my_numeric)\n",
"typeof(my_numeric)\n",
"class(my_numeric)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Class de my_numeric\n",
"\" mode et classe my_integer\"\n",
"mode(my_integer)\n",
"typeof(my_integer)\n",
"class(my_integer)\n",
"\n",
"xx <- my_numeric + my_integer\n",
"xx\n",
"mode(xx)\n",
"class(xx)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
},
{
"data": {
"text/html": [
"' mode et classe my_character'"
"4"
],
"text/latex": [
"' mode et classe my\\_character'"
"4"
],
"text/markdown": [
"' mode et classe my_character'"
"4"
],
"text/plain": [
"[1] \" mode et classe my_character\""
"[1] 4"
]
},
"metadata": {},
......@@ -584,16 +340,16 @@
{
"data": {
"text/html": [
"'character'"
"1.94591014905531"
],
"text/latex": [
"'character'"
"1.94591014905531"
],
"text/markdown": [
"'character'"
"1.94591014905531"
],
"text/plain": [
"[1] \"character\""
"[1] 1.94591"
]
},
"metadata": {},
......@@ -602,16 +358,31 @@
{
"data": {
"text/html": [
"'character'"
"<ol class=list-inline>\n",
"\t<li>0.716547878459096</li>\n",
"\t<li>0.827923827804625</li>\n",
"\t<li>0.607921322109178</li>\n",
"\t<li>0.211772634647787</li>\n",
"</ol>\n"
],
"text/latex": [
"'character'"
"\\begin{enumerate*}\n",
"\\item 0.716547878459096\n",
"\\item 0.827923827804625\n",
"\\item 0.607921322109178\n",
"\\item 0.211772634647787\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"'character'"
"1. 0.716547878459096\n",
"2. 0.827923827804625\n",
"3. 0.607921322109178\n",
"4. 0.211772634647787\n",
"\n",
"\n"
],
"text/plain": [
"[1] \"character\""
"[1] 0.7165479 0.8279238 0.6079213 0.2117726"
]
},
"metadata": {},
......@@ -620,16 +391,28 @@
{
"data": {
"text/html": [
"'character'"
"'espace de travail'"
],
"text/latex": [
"'character'"
"'espace de travail'"
],
"text/markdown": [
"'character'"
"'espace de travail'"
],
"text/plain": [
"[1] \"character\""
"[1] \"espace de travail\""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [],
"text/latex": [],
"text/markdown": [],
"text/plain": [
"character(0)"
]
},
"metadata": {},
......@@ -637,46 +420,67 @@
}
],
"source": [
"# Class de my_character\n",
"\" mode et classe my_character\"\n",
"mode(my_character)\n",
"typeof(my_character)\n",
"class(my_character)"
"# R comme calculatrice\n",
"2+3\n",
"3-2\n",
"2^3\n",
"13 %% 3\n",
"13 %/% 3\n",
"log(7) #fonction prédéfinie de R\n",
"runif(4)\n",
"\"espace de travail\"\n",
"ls() # fournit le nom des objets de l'espace de travail"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"cell_type": "markdown",
"metadata": {},
"source": [
"# my_character + 10 # argument non numérique pour un opérateur binaire\n",
"paste(my_character, \"euros\") # Concatène après conversion en caractères\n",
"b <- paste(my_character, 14)\n",
"mode(b)\n",
"class(b)"
"## Programmation sous R - Espace de travail\n",
"\n",
"Un concept de base en programmation s'appelle une **variable**.\n",
"\n",
"Une variable vous permet de **stocker une valeur** (par exemple 4) ou **un objet** (par exemple une description de fonction) à l’aide de l’*opérateur d’assignation* \n",
"\n",
" opérateur d'assignation ** \" <- \"** dans R. \n",
"\n",
"Vous pouvez ensuite utiliser le nom de cette variable pour accéder facilement à la valeur ou à l'objet stocké dans cette variable.\n",
"\n",
"On peut retrouver l'ensemble des objets stockés avec la fonction **ls()** dans la console ou dans l'onglet **Environment** de RStudio.\n",
"\n",
"On essaie et on observe...\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [],
"text/latex": [],
"text/markdown": [],
"text/plain": [
"character(0)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"' mode et classe my_logical'"
"5"
],
"text/latex": [
"' mode et classe my\\_logical'"
"5"
],
"text/markdown": [
"' mode et classe my_logical'"
"5"
],
"text/plain": [
"[1] \" mode et classe my_logical\""
"[1] 5"
]
},
"metadata": {},
......@@ -685,16 +489,34 @@
{
"data": {
"text/html": [
"'logical'"
"<ol class=list-inline>\n",
"\t<li>0.62501686764881</li>\n",
"\t<li>0.938050465425476</li>\n",
"\t<li>0.316431388491765</li>\n",
"\t<li>0.902880884939805</li>\n",
"\t<li>0.355515625793487</li>\n",
"</ol>\n"
],
"text/latex": [
"'logical'"
"\\begin{enumerate*}\n",
"\\item 0.62501686764881\n",
"\\item 0.938050465425476\n",
"\\item 0.316431388491765\n",
"\\item 0.902880884939805\n",
"\\item 0.355515625793487\n",
"\\end{enumerate*}\n"
],
"text/markdown": [
"'logical'"
"1. 0.62501686764881\n",
"2. 0.938050465425476\n",
"3. 0.316431388491765\n",
"4. 0.902880884939805\n",
"5. 0.355515625793487\n",
"\n",
"\n"
],
"text/plain": [
"[1] \"logical\""
"[1] 0.6250169 0.9380505 0.3164314 0.9028809 0.3555156"
]
},
"metadata": {},
......@@ -703,71 +525,34 @@
{
"data": {
"text/html": [
"'logical'"
"15"
],
"text/latex": [
"'logical'"
"15"
],
"text/markdown": [
"'logical'"
"15"
],
"text/plain": [
"[1] \"logical\""
"[1] 15"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Class de my_logical\n",
"\" mode et classe my_logical\"\n",
"mode(my_logical)\n",
"class(my_logical)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Tester/ concertir le mode d'un objet\n",
"\n",
"* Il est possible de tester le **mode** d'un objet **x** de R avec les fonctions:\n",
" - **is.null**(x)\n",
" - **is.numeric**(x)\n",
" - **is.logical**(x)\n",
" - **is.character**(x)\n",
" - **is.complex**(x)\n",
" \n",
"Le résultat de ces fonctions est un booléen qui prend les valeurs **TRUE** et **FALSE**\n",
"\n",
"* Il est possible de convertir un objet d'un mode à un autre avec les fonctions:\n",
" - **as.numeric**(x)\n",
" - **as.logical**(x)\n",
" - **as.character**(x)\n",
" - **as.complex**(x)\n",
" \n",
"Il faut rester prudent quant à la signification de ces conversions, R retourne toujours un résultat même si la conversion n'a pas de sens..."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
},
{
"data": {
"text/html": [
"'logical'"
"'Espace de travail 1'"
],
"text/latex": [
"'logical'"
"'Espace de travail 1'"
],
"text/markdown": [
"'logical'"
"'Espace de travail 1'"
],
"text/plain": [
"[1] \"logical\""
"[1] \"Espace de travail 1\""
]
},
"metadata": {},
......@@ -776,16 +561,31 @@
{
"data": {
"text/html": [
"'numeric'"