{ "cells": [ { "cell_type": "markdown", "id": "b646f738-c7ee-488d-85b7-07110911e466", "metadata": {}, "source": [ "# **
Diplomado en Ciencia de Datos**"
]
},
{
"cell_type": "markdown",
"id": "1d20e2c1-e091-4525-b49a-37d1f549319e",
"metadata": {},
"source": [
"# ** Introducción a R**"
]
},
{
"cell_type": "markdown",
"id": "ba199989-7b68-4d56-9854-360c062b34a9",
"metadata": {},
"source": [
"# **Profesores**"
]
},
{
"cell_type": "markdown",
"id": "a81ba6a3-e7ae-4ce3-858e-c7256b5a669a",
"metadata": {},
"source": [
"* Álvaro Montenegro, PhD, Ejercicio**"
]
},
{
"cell_type": "markdown",
"id": "dda71417-daba-40c2-bb08-315ebd7cb56b",
"metadata": {},
"source": [
"Reescriba los dos ejemplos anteriores cambiado de `while` a `for` y viceversa, según el caso."
]
},
{
"cell_type": "markdown",
"id": "86c8bd09-3b4b-4e84-a878-9f051b1c5344",
"metadata": {},
"source": [
"### **Sentencia break**\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "4a2a37db-fcd0-42c2-b30e-df074d9d9783",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12345"
]
}
],
"source": [
"for (i in 1:10) {\n",
" cat(i)\n",
" if (i == 5) break\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "731cebcc-f438-420e-a1a7-2dd290e55613",
"metadata": {},
"source": [
"## **Estructuras de control anidadas**"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "dca61b2b-d787-4c7e-8edc-768a34d23605",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x = 5 y = 3\n",
"Cinco es mayor que tres\n",
"1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , \n",
"x = 9.9 , y = 2"
]
}
],
"source": [
"x = 5\n",
"y = 3\n",
"cat(\"x =\", x, \"y =\", y)\n",
"if (x > y){ \n",
" cat(\"\\nCinco es mayor que tres\\n\")\n",
" for (i in 1:10) cat(i, end=\", \")\n",
" x = 9.9\n",
"}\n",
"y = 2\n",
"cat(\"\\nx =\", x, \", y =\", y)"
]
},
{
"cell_type": "markdown",
"id": "e2e43253-8a3a-413d-955d-e9a7ce8e8447",
"metadata": {},
"source": [
"#### **Ejemplo de las tablas de multiplicar**"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "cb658881-087b-42a7-b583-b48e20bb621e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Algunas Tablas de Multiplicar:\n",
"\n",
"Tabla del 6 \n",
"6x1 = 6\n",
"6x2 = 12\n",
"6x3 = 18\n",
"6x4 = 24\n",
"6x5 = 30\n",
"6x6 = 36\n",
"6x7 = 42\n",
"6x8 = 48\n",
"6x9 = 54\n",
"6x10 = 60\n",
"\n",
"Tabla del 7 \n",
"7x1 = 7\n",
"7x2 = 14\n",
"7x3 = 21\n",
"7x4 = 28\n",
"7x5 = 35\n",
"7x6 = 42\n",
"7x7 = 49\n",
"7x8 = 56\n",
"7x9 = 63\n",
"7x10 = 70\n",
"\n",
"Tabla del 8 \n",
"8x1 = 8\n",
"8x2 = 16\n",
"8x3 = 24\n",
"8x4 = 32\n",
"8x5 = 40\n",
"8x6 = 48\n",
"8x7 = 56\n",
"8x8 = 64\n",
"8x9 = 72\n",
"8x10 = 80\n"
]
}
],
"source": [
"cat(\"\\nAlgunas Tablas de Multiplicar:\\n\")\n",
"for (i in 6:8) { # i va aumentado desde 1, luego va a 2,3,...\n",
" cat('\\nTabla del', i,'\\n')\n",
" for (j in 1:10) cat(i, \"x\", j, \" = \", i*j,\"\\n\", sep='')\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "4d096e30-d67f-46b3-a42a-41cf817bdbe2",
"metadata": {},
"source": [
"#### **Ejemplo de una sucesión creciente de asteríscos**"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "fe9b7f5f-6f09-46b0-9e4f-0774e052fdfb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"* \n",
"* * \n",
"* * * \n",
"* * * * \n",
"* * * * * \n",
"* * * * * * \n",
"* * * * * * * \n",
"* * * * * * * * \n",
"* * * * * * * * * \n",
"* * * * * * * * * * \n",
"* * * * * * * * * * * \n"
]
}
],
"source": [
"# Sucesión creciente de asteríscos\n",
"num_lineas = 11\n",
"for (i in 1:num_lineas) { \n",
" for (j in 1:i) cat('*', end='') \n",
" cat('\\n') \n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1a43775c-a2a9-48f0-a26e-b68025701e3a",
"metadata": {},
"source": [
"# Funciones en R"
]
},
{
"cell_type": "markdown",
"id": "16eb5261-4cea-4676-8c97-4f52196be032",
"metadata": {},
"source": [
"## ** Funciones predefinidas**"
]
},
{
"cell_type": "markdown",
"id": "677c6a3a-14ca-4e45-8913-66223ded9409",
"metadata": {},
"source": [
"Están en las librerías básicas de R es decir en las que se instalan en R y que que están disponibles en cualquier sesión de trabajo, por ejemplo *sin* y *round*, *cat*:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "7335f15f-3d71-4ed9-864f-f7152173db0b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"0"
],
"text/latex": [
"0"
],
"text/markdown": [
"0"
],
"text/plain": [
"[1] 0"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"0.7071"
],
"text/latex": [
"0.7071"
],
"text/markdown": [
"0.7071"
],
"text/plain": [
"[1] 0.7071"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"angulo: 0 0.3927 0.7854 1.1781 1.5708 1.9635 2.3562 2.7489 3.1416\n",
"seno: 0 0.3827 0.7071 0.9239 1 0.9239 0.7071 0.3827 0"
]
}
],
"source": [
"round(sin(pi),4)\n",
"round(sin(pi/4),4)\n",
"# en un vector de más dimensiones\n",
"angulo = seq(0,pi,pi/8)\n",
"cat(\"\\nangulo:\", round(angulo,4))\n",
"cat(\"\\nseno:\",round(sin(angulo),4))"
]
},
{
"cell_type": "markdown",
"id": "a5e74973-9e0a-4210-b8a6-5ecb1f7bbaa7",
"metadata": {},
"source": [
"#### **Ejemplo función *rbind***"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "390f555a-18e7-40e2-bd88-1f5c9e4f9abd",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n"
],
"text/latex": [
"A matrix: 2 × 9 of type dbl\n",
"\\begin{tabular}{r|lllllllll}\n",
"\tangulo & 0 & 0.3927 & 0.7854 & 1.1781 & 1.5708 & 1.9635 & 2.3562 & 2.7489 & 3.1416\\\\\n",
"\tseno & 0 & 0.3827 & 0.7071 & 0.9239 & 1.0000 & 0.9239 & 0.7071 & 0.3827 & 0.0000\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A matrix: 2 × 9 of type dbl\n",
"\n",
"| angulo | 0 | 0.3927 | 0.7854 | 1.1781 | 1.5708 | 1.9635 | 2.3562 | 2.7489 | 3.1416 |\n",
"| seno | 0 | 0.3827 | 0.7071 | 0.9239 | 1.0000 | 0.9239 | 0.7071 | 0.3827 | 0.0000 |\n",
"\n"
],
"text/plain": [
" [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] \n",
"angulo 0 0.3927 0.7854 1.1781 1.5708 1.9635 2.3562 2.7489 3.1416\n",
"seno 0 0.3827 0.7071 0.9239 1.0000 0.9239 0.7071 0.3827 0.0000"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"rbind(angulo=round(angulo,4),seno=round(sin(angulo),4))"
]
},
{
"cell_type": "markdown",
"id": "25416639-7c74-4def-a191-2222866c8ebf",
"metadata": {},
"source": [
"## ** Creación de funciones en R**"
]
},
{
"cell_type": "markdown",
"id": "bb71c899-c93a-4806-9900-6a0bc989246a",
"metadata": {},
"source": [
"#### **Ejemplo función suma:**"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "cd68d14a-1150-4dfe-a0ea-29a6f39c6885",
"metadata": {},
"outputs": [],
"source": [
"suma = function(x,y){\n",
" s = x + y\n",
" return(s)\n",
" }"
]
},
{
"cell_type": "markdown",
"id": "c0fc9502-f0c9-4556-a65a-2414002a1da1",
"metadata": {},
"source": [
"Utilizándolo para sumar 3 y 5:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "1fc4f0b0-46f1-4ead-932b-200a11acab17",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"8"
],
"text/latex": [
"8"
],
"text/markdown": [
"8"
],
"text/plain": [
"[1] 8"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"suma(3,5)"
]
},
{
"cell_type": "markdown",
"id": "d50b062c-cb8f-4d47-9c83-f8aef831dbd7",
"metadata": {},
"source": [
"#### **Una función que regresa dos valores**"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "602a6f68-12ea-4ef6-b99d-eb3738ee56cc",
"metadata": {},
"outputs": [],
"source": [
"mysum_prod = function(x,y){\n",
" s = x + y\n",
" p = x * y\n",
" return(rbind(suma=s, producto=p))\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "1cb5d88c-9dfc-4d24-ab8e-5a5462225abe",
"metadata": {},
"source": [
"$3+5$ y $3\\times 5$"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "051ab74b-2bc7-44d6-9653-0117164bc62f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
" \n",
"\tangulo 0 0.3927 0.7854 1.1781 1.5708 1.9635 2.3562 2.7489 3.1416 \n",
"\n",
"seno 0 0.3827 0.7071 0.9239 1.0000 0.9239 0.7071 0.3827 0.0000 \n",
"
\n"
],
"text/latex": [
"A matrix: 2 × 1 of type dbl\n",
"\\begin{tabular}{r|l}\n",
"\tsuma & 8\\\\\n",
"\tproducto & 15\\\\\n",
"\\end{tabular}\n"
],
"text/markdown": [
"\n",
"A matrix: 2 × 1 of type dbl\n",
"\n",
"| suma | 8 |\n",
"| producto | 15 |\n",
"\n"
],
"text/plain": [
" [,1]\n",
"suma 8 \n",
"producto 15 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mysum_prod(3,5)"
]
},
{
"cell_type": "markdown",
"id": "1061a49b-5820-4655-bfed-51a60abf9add",
"metadata": {},
"source": [
"### **Seguir practicando R**"
]
},
{
"cell_type": "markdown",
"id": "b7fd4e32-69da-47cf-aa55-b052765c417a",
"metadata": {},
"source": [
"El menú de RStudio permite entrar directamente a la documentación para seguir avanzado y utilizando R. Además el menú grafico tiene los comandos más utilizados en R y RStudio.\n",
"\n",
"Se está utilizando mucho, por ejemplo, para producir informes seriados, por ejemplo diarios, cambiando el archivo de datos."
]
}
],
"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": "4.3.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
\n",
"\tsuma 8 \n",
"\n",
"producto 15