├── .DS_Store ├── 1 Introduccion a Python └── Introducción_a_básicos_de_Python.ipynb ├── 10 Visualizaciones avanzadas con ggplot2 en R └── Visualizacion avanzada ggplot2.R ├── 11 Series temporales en R ├── Series temporales con R.R ├── msftPrices.csv └── sbuxPrices.csv ├── 12 Mapas en R ├── Mapas en R.R └── italy map R.png ├── 2 Introduccion a NumPy └── Introducción_a_NumPy.ipynb ├── 3 Introduccion a Pandas ├── Introducción_a_Pandas.ipynb └── companies79.csv ├── 4 Introduccion a Matplotlib └── Visualización_con_Matplotlib_y_Seaborn.ipynb ├── 4.2 EXTRA Librerias EDA ├── Extra_Librerías_para_EDA.ipynb └── Xeek_Well_15-9-15.csv ├── 5 Introduccion a las series temporales ├── Index2018.csv └── Introducción_a_las_series_temporales_en_Python.ipynb ├── 6 Mapas en Python ├── Datos.zip └── Mapas.ipynb ├── 7 Introduccion a R └── Introduccion a R.R ├── 8 Importar datos en R ├── Importar datos en R.R └── cancerdata.csv ├── 9 Visualizaciones basicas en R └── Visualizaciones basicas en R.R └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecabestadistica/curso-R-Python-data-science-analisis-datos/e5a67aab9fb39915dd039fd7debbba36200442b7/.DS_Store -------------------------------------------------------------------------------- /1 Introduccion a Python/Introducción_a_básicos_de_Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Introducción a básicos de Python.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "toc_visible": true 10 | }, 11 | "kernelspec": { 12 | "display_name": "Python 3", 13 | "language": "python", 14 | "name": "python3" 15 | }, 16 | "language_info": { 17 | "codemirror_mode": { 18 | "name": "ipython", 19 | "version": 3 20 | }, 21 | "file_extension": ".py", 22 | "mimetype": "text/x-python", 23 | "name": "python", 24 | "nbconvert_exporter": "python", 25 | "pygments_lexer": "ipython3", 26 | "version": "3.7.5" 27 | } 28 | }, 29 | "cells": [ 30 | { 31 | "cell_type": "markdown", 32 | "metadata": { 33 | "id": "cWpd6kfHGz4d" 34 | }, 35 | "source": [ 36 | "# Sección 1. Números, strings, listas\n" 37 | ] 38 | }, 39 | { 40 | "cell_type": "code", 41 | "metadata": { 42 | "id": "GSXoz-J8G6Kg", 43 | "colab": { 44 | "base_uri": "https://localhost:8080/" 45 | }, 46 | "outputId": "c5dcc1b5-549f-418d-ebe3-8802f4f57503" 47 | }, 48 | "source": [ 49 | "#String\n", 50 | "print('Abcdef')" 51 | ], 52 | "execution_count": 1, 53 | "outputs": [ 54 | { 55 | "output_type": "stream", 56 | "name": "stdout", 57 | "text": [ 58 | "Abcdef\n" 59 | ] 60 | } 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "source": [ 66 | "#Número\n", 67 | "print(3)" 68 | ], 69 | "metadata": { 70 | "colab": { 71 | "base_uri": "https://localhost:8080/" 72 | }, 73 | "id": "YyMQsDESbGKt", 74 | "outputId": "b5cc8cd5-165b-4d87-d07d-67392d560f2b" 75 | }, 76 | "execution_count": 2, 77 | "outputs": [ 78 | { 79 | "output_type": "stream", 80 | "name": "stdout", 81 | "text": [ 82 | "3\n" 83 | ] 84 | } 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "metadata": { 90 | "id": "peMb8v-Vf_g8", 91 | "colab": { 92 | "base_uri": "https://localhost:8080/" 93 | }, 94 | "outputId": "a656dd02-c704-4ab7-eecc-82dfad791284" 95 | }, 96 | "source": [ 97 | "#Operación numérica\n", 98 | "print(3 + 5)" 99 | ], 100 | "execution_count": 3, 101 | "outputs": [ 102 | { 103 | "output_type": "stream", 104 | "name": "stdout", 105 | "text": [ 106 | "8\n" 107 | ] 108 | } 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "source": [ 114 | "#Mismo resultado\n", 115 | "3 + 5" 116 | ], 117 | "metadata": { 118 | "colab": { 119 | "base_uri": "https://localhost:8080/" 120 | }, 121 | "id": "DMRP9z5xbMia", 122 | "outputId": "a3f12f7c-a65c-484b-9564-0a5571f12fc5" 123 | }, 124 | "execution_count": 4, 125 | "outputs": [ 126 | { 127 | "output_type": "execute_result", 128 | "data": { 129 | "text/plain": [ 130 | "8" 131 | ] 132 | }, 133 | "metadata": {}, 134 | "execution_count": 4 135 | } 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "metadata": { 141 | "id": "YiWfMh7GHL3Q", 142 | "colab": { 143 | "base_uri": "https://localhost:8080/" 144 | }, 145 | "outputId": "30da72d1-feb3-4c0a-bfc0-8d3d38b38934" 146 | }, 147 | "source": [ 148 | "print('Alice, ' + 'Bob')" 149 | ], 150 | "execution_count": 5, 151 | "outputs": [ 152 | { 153 | "output_type": "stream", 154 | "name": "stdout", 155 | "text": [ 156 | "Alice, Bob\n" 157 | ] 158 | } 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "metadata": { 164 | "id": "jSbwq94YGz4g", 165 | "colab": { 166 | "base_uri": "https://localhost:8080/" 167 | }, 168 | "outputId": "be740098-e77a-4e95-dd08-b31b2a055d9d" 169 | }, 170 | "source": [ 171 | "print('Hello')\n", 172 | "print('Alice' + ' and Bob')" 173 | ], 174 | "execution_count": 6, 175 | "outputs": [ 176 | { 177 | "output_type": "stream", 178 | "name": "stdout", 179 | "text": [ 180 | "Hello\n", 181 | "Alice and Bob\n" 182 | ] 183 | } 184 | ] 185 | }, 186 | { 187 | "cell_type": "code", 188 | "source": [ 189 | "#Operaciones numéricas\n", 190 | "print(3 + 5)\n", 191 | "print(3 - 5)\n", 192 | "print(3 * 5)\n", 193 | "print(3 ** 3)\n", 194 | "print(3 / 5)" 195 | ], 196 | "metadata": { 197 | "colab": { 198 | "base_uri": "https://localhost:8080/" 199 | }, 200 | "id": "Hs22Z3Jmb4m1", 201 | "outputId": "0444a5db-2510-420d-f1c0-f375ce042ebe" 202 | }, 203 | "execution_count": 7, 204 | "outputs": [ 205 | { 206 | "output_type": "stream", 207 | "name": "stdout", 208 | "text": [ 209 | "8\n", 210 | "-2\n", 211 | "15\n", 212 | "27\n", 213 | "0.6\n" 214 | ] 215 | } 216 | ] 217 | }, 218 | { 219 | "cell_type": "code", 220 | "source": [ 221 | "#Lista\n", 222 | "countries = ['Portugal','Spain','United Kingdom']\n", 223 | "print(countries)" 224 | ], 225 | "metadata": { 226 | "colab": { 227 | "base_uri": "https://localhost:8080/" 228 | }, 229 | "id": "AIhC0IE0cNhy", 230 | "outputId": "2c2ea174-ef72-4b52-d660-156d307fbed6" 231 | }, 232 | "execution_count": 8, 233 | "outputs": [ 234 | { 235 | "output_type": "stream", 236 | "name": "stdout", 237 | "text": [ 238 | "['Portugal', 'Spain', 'United Kingdom']\n" 239 | ] 240 | } 241 | ] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": { 246 | "id": "FDhxgQSPGz4u" 247 | }, 248 | "source": [ 249 | "# Sección 2. Variables" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "metadata": { 255 | "id": "IlYxcbr_Gz4v" 256 | }, 257 | "source": [ 258 | "x = 1\n", 259 | "y = 2\n", 260 | "z = 3" 261 | ], 262 | "execution_count": 9, 263 | "outputs": [] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "source": [ 268 | "z+y" 269 | ], 270 | "metadata": { 271 | "colab": { 272 | "base_uri": "https://localhost:8080/" 273 | }, 274 | "id": "2iJ7WxvV4Wc_", 275 | "outputId": "1d70c91a-054e-4b87-f141-10ea6b3ea3da" 276 | }, 277 | "execution_count": 13, 278 | "outputs": [ 279 | { 280 | "output_type": "execute_result", 281 | "data": { 282 | "text/plain": [ 283 | "5" 284 | ] 285 | }, 286 | "metadata": {}, 287 | "execution_count": 13 288 | } 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "metadata": { 294 | "id": "fH0oNI1JGz4z", 295 | "colab": { 296 | "base_uri": "https://localhost:8080/" 297 | }, 298 | "outputId": "abf05f02-4deb-4c11-e52e-9ae3502c37af" 299 | }, 300 | "source": [ 301 | "print(x+y+z)" 302 | ], 303 | "execution_count": 14, 304 | "outputs": [ 305 | { 306 | "output_type": "stream", 307 | "name": "stdout", 308 | "text": [ 309 | "6\n" 310 | ] 311 | } 312 | ] 313 | }, 314 | { 315 | "cell_type": "code", 316 | "metadata": { 317 | "id": "4sRpZi07HEA6" 318 | }, 319 | "source": [ 320 | "#Otra opción para asignar valores a variables en 1 línea\n", 321 | "x, y, z = 9, 10, 11" 322 | ], 323 | "execution_count": 19, 324 | "outputs": [] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "source": [ 329 | "z" 330 | ], 331 | "metadata": { 332 | "colab": { 333 | "base_uri": "https://localhost:8080/" 334 | }, 335 | "id": "oa1-m-gM4q_o", 336 | "outputId": "8faa2653-8d0e-4a99-b1c9-a2a7af9e0d5e" 337 | }, 338 | "execution_count": 22, 339 | "outputs": [ 340 | { 341 | "output_type": "execute_result", 342 | "data": { 343 | "text/plain": [ 344 | "11" 345 | ] 346 | }, 347 | "metadata": {}, 348 | "execution_count": 22 349 | } 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "metadata": { 355 | "id": "3K2tX7TrGeG8", 356 | "colab": { 357 | "base_uri": "https://localhost:8080/" 358 | }, 359 | "outputId": "3550442a-7cbe-4fa3-efbc-05a635f2c467" 360 | }, 361 | "source": [ 362 | "# Operator suma (+) con strings:\n", 363 | "s = 'a'\n", 364 | "print(s + 'd')" 365 | ], 366 | "execution_count": 23, 367 | "outputs": [ 368 | { 369 | "output_type": "stream", 370 | "name": "stdout", 371 | "text": [ 372 | "ad\n" 373 | ] 374 | } 375 | ] 376 | }, 377 | { 378 | "cell_type": "code", 379 | "metadata": { 380 | "id": "rTURJu5glVp8", 381 | "colab": { 382 | "base_uri": "https://localhost:8080/" 383 | }, 384 | "outputId": "6895bcb3-3a7b-4152-81d6-b56b5950f160" 385 | }, 386 | "source": [ 387 | "#Tipos de variables\n", 388 | "print(type(1))\n", 389 | "print(type('abc'))\n", 390 | "print(type(countries))" 391 | ], 392 | "execution_count": 24, 393 | "outputs": [ 394 | { 395 | "output_type": "stream", 396 | "name": "stdout", 397 | "text": [ 398 | "\n", 399 | "\n", 400 | "\n" 401 | ] 402 | } 403 | ] 404 | }, 405 | { 406 | "cell_type": "code", 407 | "metadata": { 408 | "id": "xvKmCQA13ZTT", 409 | "colab": { 410 | "base_uri": "https://localhost:8080/" 411 | }, 412 | "outputId": "7ac5b7a8-c116-4013-bbaa-2213b19e85a0" 413 | }, 414 | "source": [ 415 | "# Las variables de un tipo se pueden convertir a otro tipo\n", 416 | "# Convertir un float a entero:\n", 417 | "\n", 418 | "x = 3.9\n", 419 | "print(x)\n", 420 | "print(type(x))\n", 421 | "\n", 422 | "y = int(x)\n", 423 | "print(y)\n", 424 | "print(type(y))\n" 425 | ], 426 | "execution_count": 27, 427 | "outputs": [ 428 | { 429 | "output_type": "stream", 430 | "name": "stdout", 431 | "text": [ 432 | "3.9\n", 433 | "\n", 434 | "3\n", 435 | "\n" 436 | ] 437 | } 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "source": [ 443 | "# Convertir un string a entero:\n", 444 | "x = '3'\n", 445 | "print(x)\n", 446 | "print(type(x))\n", 447 | "\n", 448 | "y = int(x)\n", 449 | "print(y)\n", 450 | "print(type(y))" 451 | ], 452 | "metadata": { 453 | "colab": { 454 | "base_uri": "https://localhost:8080/" 455 | }, 456 | "id": "g5CvWGPPdlRN", 457 | "outputId": "35f32b0a-ebb4-432e-b24c-2e3506788d03" 458 | }, 459 | "execution_count": 31, 460 | "outputs": [ 461 | { 462 | "output_type": "stream", 463 | "name": "stdout", 464 | "text": [ 465 | "3\n", 466 | "\n", 467 | "3\n", 468 | "\n" 469 | ] 470 | } 471 | ] 472 | }, 473 | { 474 | "cell_type": "code", 475 | "source": [ 476 | "# Convertir un entero a string:\n", 477 | "x = 30\n", 478 | "print(x)\n", 479 | "print(type(x))\n", 480 | "\n", 481 | "y = str(30)\n", 482 | "print(y)\n", 483 | "print(type(y))" 484 | ], 485 | "metadata": { 486 | "colab": { 487 | "base_uri": "https://localhost:8080/" 488 | }, 489 | "id": "POsy_AUVdlpK", 490 | "outputId": "09d2a958-cda8-4823-f8ab-007666c47a4a" 491 | }, 492 | "execution_count": 32, 493 | "outputs": [ 494 | { 495 | "output_type": "stream", 496 | "name": "stdout", 497 | "text": [ 498 | "30\n", 499 | "\n", 500 | "30\n", 501 | "\n" 502 | ] 503 | } 504 | ] 505 | }, 506 | { 507 | "cell_type": "markdown", 508 | "metadata": { 509 | "id": "jqqPgZfHFCMp" 510 | }, 511 | "source": [ 512 | "# Sección 3. Operadores aritméticos en variables" 513 | ] 514 | }, 515 | { 516 | "cell_type": "code", 517 | "metadata": { 518 | "id": "cQ4hadQoj3H0", 519 | "colab": { 520 | "base_uri": "https://localhost:8080/" 521 | }, 522 | "outputId": "2296f9a3-d627-46b6-d636-8ee346135b9b" 523 | }, 524 | "source": [ 525 | "# El valor de una variable puede modificarse usando operadores\n", 526 | "x = 3\n", 527 | "x = x + 1\n", 528 | "print(x)" 529 | ], 530 | "execution_count": 33, 531 | "outputs": [ 532 | { 533 | "output_type": "stream", 534 | "name": "stdout", 535 | "text": [ 536 | "4\n" 537 | ] 538 | } 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "metadata": { 544 | "id": "YNcf-uF0j-Ec", 545 | "colab": { 546 | "base_uri": "https://localhost:8080/" 547 | }, 548 | "outputId": "13918c1e-dc26-4086-a61a-dbe543ef5a78" 549 | }, 550 | "source": [ 551 | "# Versión simplificada\n", 552 | "x = 3\n", 553 | "x += 1\n", 554 | "print(x)" 555 | ], 556 | "execution_count": 34, 557 | "outputs": [ 558 | { 559 | "output_type": "stream", 560 | "name": "stdout", 561 | "text": [ 562 | "4\n" 563 | ] 564 | } 565 | ] 566 | }, 567 | { 568 | "cell_type": "code", 569 | "metadata": { 570 | "id": "nVG0Z8XSkCTV", 571 | "colab": { 572 | "base_uri": "https://localhost:8080/" 573 | }, 574 | "outputId": "8bef3914-ea73-4824-995b-159493f0dafa" 575 | }, 576 | "source": [ 577 | "# También vale para otros operadores\n", 578 | "x = 3\n", 579 | "x -= 1\n", 580 | "print(x)" 581 | ], 582 | "execution_count": 35, 583 | "outputs": [ 584 | { 585 | "output_type": "stream", 586 | "name": "stdout", 587 | "text": [ 588 | "2\n" 589 | ] 590 | } 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "metadata": { 596 | "id": "Q46cW591kI0L", 597 | "colab": { 598 | "base_uri": "https://localhost:8080/" 599 | }, 600 | "outputId": "21593f77-145e-43da-f771-188840a71d77" 601 | }, 602 | "source": [ 603 | "x = 3\n", 604 | "x *= 2\n", 605 | "print(x)" 606 | ], 607 | "execution_count": 36, 608 | "outputs": [ 609 | { 610 | "output_type": "stream", 611 | "name": "stdout", 612 | "text": [ 613 | "6\n" 614 | ] 615 | } 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "metadata": { 621 | "id": "wAKslcL1kLBy", 622 | "colab": { 623 | "base_uri": "https://localhost:8080/" 624 | }, 625 | "outputId": "1cba2465-c239-42cb-b18e-5e456e3b0346" 626 | }, 627 | "source": [ 628 | "x = 3\n", 629 | "x /= 2\n", 630 | "print(x)" 631 | ], 632 | "execution_count": 37, 633 | "outputs": [ 634 | { 635 | "output_type": "stream", 636 | "name": "stdout", 637 | "text": [ 638 | "1.5\n" 639 | ] 640 | } 641 | ] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "source": [ 646 | "# Sección 4. Cadenas de texto" 647 | ], 648 | "metadata": { 649 | "id": "XSuH6naeoXUU" 650 | } 651 | }, 652 | { 653 | "cell_type": "code", 654 | "source": [ 655 | "mensaje='Soy alumno del Curso de Python'\n", 656 | "print(mensaje)" 657 | ], 658 | "metadata": { 659 | "colab": { 660 | "base_uri": "https://localhost:8080/" 661 | }, 662 | "id": "DZ2-Rw41oVXa", 663 | "outputId": "219619aa-dcd0-4cfd-be5a-f54d52e65f8c" 664 | }, 665 | "execution_count": 38, 666 | "outputs": [ 667 | { 668 | "output_type": "stream", 669 | "name": "stdout", 670 | "text": [ 671 | "Soy alumno del Curso de Python\n" 672 | ] 673 | } 674 | ] 675 | }, 676 | { 677 | "cell_type": "code", 678 | "source": [ 679 | "mensaje[0]" 680 | ], 681 | "metadata": { 682 | "colab": { 683 | "base_uri": "https://localhost:8080/", 684 | "height": 35 685 | }, 686 | "id": "N_RuLTtqokui", 687 | "outputId": "5b53b33f-c3fb-4cf7-b504-d2d9a3ce3149" 688 | }, 689 | "execution_count": 39, 690 | "outputs": [ 691 | { 692 | "output_type": "execute_result", 693 | "data": { 694 | "text/plain": [ 695 | "'S'" 696 | ], 697 | "application/vnd.google.colaboratory.intrinsic+json": { 698 | "type": "string" 699 | } 700 | }, 701 | "metadata": {}, 702 | "execution_count": 39 703 | } 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "source": [ 709 | "mensaje[1]" 710 | ], 711 | "metadata": { 712 | "colab": { 713 | "base_uri": "https://localhost:8080/", 714 | "height": 35 715 | }, 716 | "id": "9u0v9l7JomyA", 717 | "outputId": "1f95f25d-f501-4305-a87b-ed415c472080" 718 | }, 719 | "execution_count": 40, 720 | "outputs": [ 721 | { 722 | "output_type": "execute_result", 723 | "data": { 724 | "text/plain": [ 725 | "'o'" 726 | ], 727 | "application/vnd.google.colaboratory.intrinsic+json": { 728 | "type": "string" 729 | } 730 | }, 731 | "metadata": {}, 732 | "execution_count": 40 733 | } 734 | ] 735 | }, 736 | { 737 | "cell_type": "code", 738 | "source": [ 739 | "mensaje[2]" 740 | ], 741 | "metadata": { 742 | "colab": { 743 | "base_uri": "https://localhost:8080/", 744 | "height": 35 745 | }, 746 | "id": "ge-S1lliooTt", 747 | "outputId": "2b5f21cb-f609-4c99-8993-4e635510117f" 748 | }, 749 | "execution_count": 41, 750 | "outputs": [ 751 | { 752 | "output_type": "execute_result", 753 | "data": { 754 | "text/plain": [ 755 | "'y'" 756 | ], 757 | "application/vnd.google.colaboratory.intrinsic+json": { 758 | "type": "string" 759 | } 760 | }, 761 | "metadata": {}, 762 | "execution_count": 41 763 | } 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "source": [ 769 | "mensaje[-1]" 770 | ], 771 | "metadata": { 772 | "colab": { 773 | "base_uri": "https://localhost:8080/", 774 | "height": 35 775 | }, 776 | "id": "UdZJimqEpDLE", 777 | "outputId": "418c13a5-3a3d-4c57-f9c2-6e9dc64583ed" 778 | }, 779 | "execution_count": 42, 780 | "outputs": [ 781 | { 782 | "output_type": "execute_result", 783 | "data": { 784 | "text/plain": [ 785 | "'n'" 786 | ], 787 | "application/vnd.google.colaboratory.intrinsic+json": { 788 | "type": "string" 789 | } 790 | }, 791 | "metadata": {}, 792 | "execution_count": 42 793 | } 794 | ] 795 | }, 796 | { 797 | "cell_type": "code", 798 | "source": [ 799 | "mensaje[-6:-1]" 800 | ], 801 | "metadata": { 802 | "colab": { 803 | "base_uri": "https://localhost:8080/", 804 | "height": 35 805 | }, 806 | "id": "5LqW4WeWoqJp", 807 | "outputId": "8932f041-677d-46f0-bf8c-42eb2cbe54c8" 808 | }, 809 | "execution_count": 43, 810 | "outputs": [ 811 | { 812 | "output_type": "execute_result", 813 | "data": { 814 | "text/plain": [ 815 | "'Pytho'" 816 | ], 817 | "application/vnd.google.colaboratory.intrinsic+json": { 818 | "type": "string" 819 | } 820 | }, 821 | "metadata": {}, 822 | "execution_count": 43 823 | } 824 | ] 825 | }, 826 | { 827 | "cell_type": "code", 828 | "source": [ 829 | "mensaje[-6:-1] + mensaje[-1]" 830 | ], 831 | "metadata": { 832 | "colab": { 833 | "base_uri": "https://localhost:8080/", 834 | "height": 35 835 | }, 836 | "id": "xQiPmsZbpIvt", 837 | "outputId": "45f867cf-6cc4-4c90-c15a-8474e5a73688" 838 | }, 839 | "execution_count": 44, 840 | "outputs": [ 841 | { 842 | "output_type": "execute_result", 843 | "data": { 844 | "text/plain": [ 845 | "'Python'" 846 | ], 847 | "application/vnd.google.colaboratory.intrinsic+json": { 848 | "type": "string" 849 | } 850 | }, 851 | "metadata": {}, 852 | "execution_count": 44 853 | } 854 | ] 855 | }, 856 | { 857 | "cell_type": "code", 858 | "source": [ 859 | "len(mensaje)" 860 | ], 861 | "metadata": { 862 | "colab": { 863 | "base_uri": "https://localhost:8080/" 864 | }, 865 | "id": "htfqM3vZpOz2", 866 | "outputId": "70a38061-d0e1-462f-dc48-b09e905abed4" 867 | }, 868 | "execution_count": 45, 869 | "outputs": [ 870 | { 871 | "output_type": "execute_result", 872 | "data": { 873 | "text/plain": [ 874 | "30" 875 | ] 876 | }, 877 | "metadata": {}, 878 | "execution_count": 45 879 | } 880 | ] 881 | }, 882 | { 883 | "cell_type": "code", 884 | "source": [ 885 | "mensaje[len(mensaje)-6:len(mensaje)]" 886 | ], 887 | "metadata": { 888 | "colab": { 889 | "base_uri": "https://localhost:8080/", 890 | "height": 35 891 | }, 892 | "id": "ZNIUiRyFpWz-", 893 | "outputId": "af0c1f2c-4e70-4841-db52-56d8e756b457" 894 | }, 895 | "execution_count": 46, 896 | "outputs": [ 897 | { 898 | "output_type": "execute_result", 899 | "data": { 900 | "text/plain": [ 901 | "'Python'" 902 | ], 903 | "application/vnd.google.colaboratory.intrinsic+json": { 904 | "type": "string" 905 | } 906 | }, 907 | "metadata": {}, 908 | "execution_count": 46 909 | } 910 | ] 911 | }, 912 | { 913 | "cell_type": "code", 914 | "source": [ 915 | "mensaje*2" 916 | ], 917 | "metadata": { 918 | "colab": { 919 | "base_uri": "https://localhost:8080/", 920 | "height": 35 921 | }, 922 | "id": "E6i1XMLapkK-", 923 | "outputId": "c9b7eed5-bf3a-493e-d979-2ef2e950636d" 924 | }, 925 | "execution_count": 47, 926 | "outputs": [ 927 | { 928 | "output_type": "execute_result", 929 | "data": { 930 | "text/plain": [ 931 | "'Soy alumno del Curso de PythonSoy alumno del Curso de Python'" 932 | ], 933 | "application/vnd.google.colaboratory.intrinsic+json": { 934 | "type": "string" 935 | } 936 | }, 937 | "metadata": {}, 938 | "execution_count": 47 939 | } 940 | ] 941 | }, 942 | { 943 | "cell_type": "code", 944 | "source": [ 945 | "'alumno' in mensaje" 946 | ], 947 | "metadata": { 948 | "colab": { 949 | "base_uri": "https://localhost:8080/" 950 | }, 951 | "id": "wZvj4nRFpm9j", 952 | "outputId": "e364d867-65dc-46c2-bda4-189ade1cdccd" 953 | }, 954 | "execution_count": 48, 955 | "outputs": [ 956 | { 957 | "output_type": "execute_result", 958 | "data": { 959 | "text/plain": [ 960 | "True" 961 | ] 962 | }, 963 | "metadata": {}, 964 | "execution_count": 48 965 | } 966 | ] 967 | }, 968 | { 969 | "cell_type": "code", 970 | "source": [ 971 | "'Curso' in mensaje" 972 | ], 973 | "metadata": { 974 | "colab": { 975 | "base_uri": "https://localhost:8080/" 976 | }, 977 | "id": "-y2wbFVqptiy", 978 | "outputId": "5f288be8-7c9a-41a9-8ba7-749a4e53e3ba" 979 | }, 980 | "execution_count": 49, 981 | "outputs": [ 982 | { 983 | "output_type": "execute_result", 984 | "data": { 985 | "text/plain": [ 986 | "True" 987 | ] 988 | }, 989 | "metadata": {}, 990 | "execution_count": 49 991 | } 992 | ] 993 | }, 994 | { 995 | "cell_type": "code", 996 | "source": [ 997 | "'curso' in mensaje" 998 | ], 999 | "metadata": { 1000 | "colab": { 1001 | "base_uri": "https://localhost:8080/" 1002 | }, 1003 | "id": "Eoa90Begpq0B", 1004 | "outputId": "48e30f7c-3c22-48aa-b6e8-6fc68106fece" 1005 | }, 1006 | "execution_count": 50, 1007 | "outputs": [ 1008 | { 1009 | "output_type": "execute_result", 1010 | "data": { 1011 | "text/plain": [ 1012 | "False" 1013 | ] 1014 | }, 1015 | "metadata": {}, 1016 | "execution_count": 50 1017 | } 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "source": [ 1023 | "'curso' not in mensaje" 1024 | ], 1025 | "metadata": { 1026 | "colab": { 1027 | "base_uri": "https://localhost:8080/" 1028 | }, 1029 | "id": "MxXO8ekqpy2U", 1030 | "outputId": "9304387f-b285-445b-f9e7-d4546b875765" 1031 | }, 1032 | "execution_count": 51, 1033 | "outputs": [ 1034 | { 1035 | "output_type": "execute_result", 1036 | "data": { 1037 | "text/plain": [ 1038 | "True" 1039 | ] 1040 | }, 1041 | "metadata": {}, 1042 | "execution_count": 51 1043 | } 1044 | ] 1045 | }, 1046 | { 1047 | "cell_type": "code", 1048 | "source": [ 1049 | "mensaje.split()" 1050 | ], 1051 | "metadata": { 1052 | "colab": { 1053 | "base_uri": "https://localhost:8080/" 1054 | }, 1055 | "id": "AdKdZfJtp4ne", 1056 | "outputId": "1196d55c-d845-40ad-f48b-070c2aa971b9" 1057 | }, 1058 | "execution_count": 52, 1059 | "outputs": [ 1060 | { 1061 | "output_type": "execute_result", 1062 | "data": { 1063 | "text/plain": [ 1064 | "['Soy', 'alumno', 'del', 'Curso', 'de', 'Python']" 1065 | ] 1066 | }, 1067 | "metadata": {}, 1068 | "execution_count": 52 1069 | } 1070 | ] 1071 | }, 1072 | { 1073 | "cell_type": "code", 1074 | "source": [ 1075 | "split_mensaje=mensaje.split()\n", 1076 | "split_mensaje[0]" 1077 | ], 1078 | "metadata": { 1079 | "colab": { 1080 | "base_uri": "https://localhost:8080/", 1081 | "height": 35 1082 | }, 1083 | "id": "ogE92Nnlp8Cm", 1084 | "outputId": "4272f6ca-b30c-454d-ad10-b68e7765e202" 1085 | }, 1086 | "execution_count": 53, 1087 | "outputs": [ 1088 | { 1089 | "output_type": "execute_result", 1090 | "data": { 1091 | "text/plain": [ 1092 | "'Soy'" 1093 | ], 1094 | "application/vnd.google.colaboratory.intrinsic+json": { 1095 | "type": "string" 1096 | } 1097 | }, 1098 | "metadata": {}, 1099 | "execution_count": 53 1100 | } 1101 | ] 1102 | }, 1103 | { 1104 | "cell_type": "code", 1105 | "source": [ 1106 | "type(split_mensaje)" 1107 | ], 1108 | "metadata": { 1109 | "colab": { 1110 | "base_uri": "https://localhost:8080/" 1111 | }, 1112 | "id": "-B86xAsJ8dXs", 1113 | "outputId": "c037c409-d414-4916-e656-a3c6cda7283d" 1114 | }, 1115 | "execution_count": 54, 1116 | "outputs": [ 1117 | { 1118 | "output_type": "execute_result", 1119 | "data": { 1120 | "text/plain": [ 1121 | "list" 1122 | ] 1123 | }, 1124 | "metadata": {}, 1125 | "execution_count": 54 1126 | } 1127 | ] 1128 | }, 1129 | { 1130 | "cell_type": "code", 1131 | "source": [ 1132 | "split_mensaje[len(split_mensaje)-1]" 1133 | ], 1134 | "metadata": { 1135 | "colab": { 1136 | "base_uri": "https://localhost:8080/", 1137 | "height": 35 1138 | }, 1139 | "id": "eLvg-qKnqBai", 1140 | "outputId": "8b988530-aa51-474f-ae52-f09379c64577" 1141 | }, 1142 | "execution_count": 55, 1143 | "outputs": [ 1144 | { 1145 | "output_type": "execute_result", 1146 | "data": { 1147 | "text/plain": [ 1148 | "'Python'" 1149 | ], 1150 | "application/vnd.google.colaboratory.intrinsic+json": { 1151 | "type": "string" 1152 | } 1153 | }, 1154 | "metadata": {}, 1155 | "execution_count": 55 1156 | } 1157 | ] 1158 | }, 1159 | { 1160 | "cell_type": "code", 1161 | "source": [ 1162 | "mensaje.startswith('Soy')" 1163 | ], 1164 | "metadata": { 1165 | "colab": { 1166 | "base_uri": "https://localhost:8080/" 1167 | }, 1168 | "id": "BbOE8GnFqLzw", 1169 | "outputId": "c4787c61-a812-42aa-e888-ce4252464256" 1170 | }, 1171 | "execution_count": 56, 1172 | "outputs": [ 1173 | { 1174 | "output_type": "execute_result", 1175 | "data": { 1176 | "text/plain": [ 1177 | "True" 1178 | ] 1179 | }, 1180 | "metadata": {}, 1181 | "execution_count": 56 1182 | } 1183 | ] 1184 | }, 1185 | { 1186 | "cell_type": "code", 1187 | "source": [ 1188 | "mensaje.endswith('Python')" 1189 | ], 1190 | "metadata": { 1191 | "colab": { 1192 | "base_uri": "https://localhost:8080/" 1193 | }, 1194 | "id": "f8cBUa6wqO7K", 1195 | "outputId": "633fc9ff-2282-4be7-fdf7-84f50e409df8" 1196 | }, 1197 | "execution_count": 57, 1198 | "outputs": [ 1199 | { 1200 | "output_type": "execute_result", 1201 | "data": { 1202 | "text/plain": [ 1203 | "True" 1204 | ] 1205 | }, 1206 | "metadata": {}, 1207 | "execution_count": 57 1208 | } 1209 | ] 1210 | }, 1211 | { 1212 | "cell_type": "code", 1213 | "source": [ 1214 | "mensaje.find('Curso')" 1215 | ], 1216 | "metadata": { 1217 | "colab": { 1218 | "base_uri": "https://localhost:8080/" 1219 | }, 1220 | "id": "ANHbD8D_qS-M", 1221 | "outputId": "47800ca5-a471-45be-e947-1d12d54efd93" 1222 | }, 1223 | "execution_count": 58, 1224 | "outputs": [ 1225 | { 1226 | "output_type": "execute_result", 1227 | "data": { 1228 | "text/plain": [ 1229 | "15" 1230 | ] 1231 | }, 1232 | "metadata": {}, 1233 | "execution_count": 58 1234 | } 1235 | ] 1236 | }, 1237 | { 1238 | "cell_type": "code", 1239 | "source": [ 1240 | "mensaje[15:20]" 1241 | ], 1242 | "metadata": { 1243 | "colab": { 1244 | "base_uri": "https://localhost:8080/", 1245 | "height": 35 1246 | }, 1247 | "id": "_rWpvDAoqbJW", 1248 | "outputId": "f966c9ed-b1c7-4b90-f51f-dc956b5412f1" 1249 | }, 1250 | "execution_count": 60, 1251 | "outputs": [ 1252 | { 1253 | "output_type": "execute_result", 1254 | "data": { 1255 | "text/plain": [ 1256 | "'Curso'" 1257 | ], 1258 | "application/vnd.google.colaboratory.intrinsic+json": { 1259 | "type": "string" 1260 | } 1261 | }, 1262 | "metadata": {}, 1263 | "execution_count": 60 1264 | } 1265 | ] 1266 | }, 1267 | { 1268 | "cell_type": "code", 1269 | "source": [ 1270 | "print(mensaje)\n", 1271 | "mensaje.count('de')" 1272 | ], 1273 | "metadata": { 1274 | "colab": { 1275 | "base_uri": "https://localhost:8080/" 1276 | }, 1277 | "id": "MCgEbjrZq0Cv", 1278 | "outputId": "e869a092-eff8-44fa-bc59-9709ad9f7b0f" 1279 | }, 1280 | "execution_count": 61, 1281 | "outputs": [ 1282 | { 1283 | "output_type": "stream", 1284 | "name": "stdout", 1285 | "text": [ 1286 | "Soy alumno del Curso de Python\n" 1287 | ] 1288 | }, 1289 | { 1290 | "output_type": "execute_result", 1291 | "data": { 1292 | "text/plain": [ 1293 | "2" 1294 | ] 1295 | }, 1296 | "metadata": {}, 1297 | "execution_count": 61 1298 | } 1299 | ] 1300 | }, 1301 | { 1302 | "cell_type": "code", 1303 | "source": [ 1304 | "print(mensaje)\n", 1305 | "mensaje = mensaje + ' de Eli'\n", 1306 | "print(mensaje)" 1307 | ], 1308 | "metadata": { 1309 | "colab": { 1310 | "base_uri": "https://localhost:8080/" 1311 | }, 1312 | "id": "Aeo8K4ndqelz", 1313 | "outputId": "c5c8cd48-9a17-4697-df90-557a396e66db" 1314 | }, 1315 | "execution_count": 62, 1316 | "outputs": [ 1317 | { 1318 | "output_type": "stream", 1319 | "name": "stdout", 1320 | "text": [ 1321 | "Soy alumno del Curso de Python\n", 1322 | "Soy alumno del Curso de Python de Eli\n" 1323 | ] 1324 | } 1325 | ] 1326 | }, 1327 | { 1328 | "cell_type": "code", 1329 | "source": [ 1330 | "mensaje.count('de')" 1331 | ], 1332 | "metadata": { 1333 | "colab": { 1334 | "base_uri": "https://localhost:8080/" 1335 | }, 1336 | "id": "9g21UNp_qnEF", 1337 | "outputId": "f5247b39-b05e-41e1-908c-0b3a3dd1d7a5" 1338 | }, 1339 | "execution_count": 63, 1340 | "outputs": [ 1341 | { 1342 | "output_type": "execute_result", 1343 | "data": { 1344 | "text/plain": [ 1345 | "3" 1346 | ] 1347 | }, 1348 | "metadata": {}, 1349 | "execution_count": 63 1350 | } 1351 | ] 1352 | }, 1353 | { 1354 | "cell_type": "code", 1355 | "source": [ 1356 | "mensaje = mensaje.replace('del', 'de el')\n" 1357 | ], 1358 | "metadata": { 1359 | "id": "00dsvjsSq-OQ" 1360 | }, 1361 | "execution_count": 68, 1362 | "outputs": [] 1363 | }, 1364 | { 1365 | "cell_type": "code", 1366 | "source": [ 1367 | "mensaje" 1368 | ], 1369 | "metadata": { 1370 | "colab": { 1371 | "base_uri": "https://localhost:8080/", 1372 | "height": 35 1373 | }, 1374 | "id": "jvPzJpix-M_U", 1375 | "outputId": "ee9af099-d735-416e-ef21-da8042349538" 1376 | }, 1377 | "execution_count": 69, 1378 | "outputs": [ 1379 | { 1380 | "output_type": "execute_result", 1381 | "data": { 1382 | "text/plain": [ 1383 | "'Soy alumno de el Curso de Python de Eli'" 1384 | ], 1385 | "application/vnd.google.colaboratory.intrinsic+json": { 1386 | "type": "string" 1387 | } 1388 | }, 1389 | "metadata": {}, 1390 | "execution_count": 69 1391 | } 1392 | ] 1393 | }, 1394 | { 1395 | "cell_type": "code", 1396 | "source": [ 1397 | "mensaje = mensaje.replace('de el', 'del')" 1398 | ], 1399 | "metadata": { 1400 | "id": "prhpisVtrHie" 1401 | }, 1402 | "execution_count": 70, 1403 | "outputs": [] 1404 | }, 1405 | { 1406 | "cell_type": "code", 1407 | "source": [ 1408 | "mensaje" 1409 | ], 1410 | "metadata": { 1411 | "colab": { 1412 | "base_uri": "https://localhost:8080/", 1413 | "height": 35 1414 | }, 1415 | "id": "RFHmRn_Z-c_X", 1416 | "outputId": "3669303a-3f8f-4bd7-eaef-20f15394c4d0" 1417 | }, 1418 | "execution_count": 71, 1419 | "outputs": [ 1420 | { 1421 | "output_type": "execute_result", 1422 | "data": { 1423 | "text/plain": [ 1424 | "'Soy alumno del Curso de Python de Eli'" 1425 | ], 1426 | "application/vnd.google.colaboratory.intrinsic+json": { 1427 | "type": "string" 1428 | } 1429 | }, 1430 | "metadata": {}, 1431 | "execution_count": 71 1432 | } 1433 | ] 1434 | }, 1435 | { 1436 | "cell_type": "code", 1437 | "source": [ 1438 | "mensaje.title() #lower, capitalize, title" 1439 | ], 1440 | "metadata": { 1441 | "colab": { 1442 | "base_uri": "https://localhost:8080/", 1443 | "height": 35 1444 | }, 1445 | "id": "EUnNuYCJrK0T", 1446 | "outputId": "4fe716b9-e83c-476e-8033-18806cfc53f7" 1447 | }, 1448 | "execution_count": 75, 1449 | "outputs": [ 1450 | { 1451 | "output_type": "execute_result", 1452 | "data": { 1453 | "text/plain": [ 1454 | "'Soy Alumno Del Curso De Python De Eli'" 1455 | ], 1456 | "application/vnd.google.colaboratory.intrinsic+json": { 1457 | "type": "string" 1458 | } 1459 | }, 1460 | "metadata": {}, 1461 | "execution_count": 75 1462 | } 1463 | ] 1464 | }, 1465 | { 1466 | "cell_type": "markdown", 1467 | "metadata": { 1468 | "id": "ni6o6HPXGz5A" 1469 | }, 1470 | "source": [ 1471 | "# Sección 5. Ciclos y funciones" 1472 | ] 1473 | }, 1474 | { 1475 | "cell_type": "code", 1476 | "metadata": { 1477 | "id": "duWN7fUGkwHi", 1478 | "colab": { 1479 | "base_uri": "https://localhost:8080/" 1480 | }, 1481 | "outputId": "e6c8d24c-cc4b-4540-ee2a-08d4677fca84" 1482 | }, 1483 | "source": [ 1484 | "# Los rangos son \"iteradores\" que se usan en los ciclos para iterar.\n", 1485 | "# range(n) produce los valores 0, 1, ..., n-1, es decir, el último valor (n) no se incluye:\n", 1486 | "for i in range(4):\n", 1487 | " print(i)" 1488 | ], 1489 | "execution_count": 76, 1490 | "outputs": [ 1491 | { 1492 | "output_type": "stream", 1493 | "name": "stdout", 1494 | "text": [ 1495 | "0\n", 1496 | "1\n", 1497 | "2\n", 1498 | "3\n" 1499 | ] 1500 | } 1501 | ] 1502 | }, 1503 | { 1504 | "cell_type": "code", 1505 | "source": [ 1506 | "# range(start, stop, step) produce valores entre 'start' y 'stop' incrementando en saltos de tamaño 'step'. \n", 1507 | "for i in range(0,10,2):\n", 1508 | " print(i)" 1509 | ], 1510 | "metadata": { 1511 | "colab": { 1512 | "base_uri": "https://localhost:8080/" 1513 | }, 1514 | "id": "U7-7G7tmgqen", 1515 | "outputId": "d347a2cc-c8a2-4e5e-e7c7-86b6bb3e30d2" 1516 | }, 1517 | "execution_count": 77, 1518 | "outputs": [ 1519 | { 1520 | "output_type": "stream", 1521 | "name": "stdout", 1522 | "text": [ 1523 | "0\n", 1524 | "2\n", 1525 | "4\n", 1526 | "6\n", 1527 | "8\n" 1528 | ] 1529 | } 1530 | ] 1531 | }, 1532 | { 1533 | "cell_type": "code", 1534 | "source": [ 1535 | "# Se puede hacer incluso hacia atrás:\n", 1536 | "for i in range(10,0,-2):\n", 1537 | " print(i)" 1538 | ], 1539 | "metadata": { 1540 | "colab": { 1541 | "base_uri": "https://localhost:8080/" 1542 | }, 1543 | "id": "ZVO5GDBBg4XX", 1544 | "outputId": "bf3d612a-fc5f-4f28-b797-a2f3397a52da" 1545 | }, 1546 | "execution_count": 78, 1547 | "outputs": [ 1548 | { 1549 | "output_type": "stream", 1550 | "name": "stdout", 1551 | "text": [ 1552 | "10\n", 1553 | "8\n", 1554 | "6\n", 1555 | "4\n", 1556 | "2\n" 1557 | ] 1558 | } 1559 | ] 1560 | }, 1561 | { 1562 | "cell_type": "code", 1563 | "source": [ 1564 | "for i in range(6, 3, -1):\n", 1565 | " print(i)" 1566 | ], 1567 | "metadata": { 1568 | "colab": { 1569 | "base_uri": "https://localhost:8080/" 1570 | }, 1571 | "id": "BeDwtwMlhBoa", 1572 | "outputId": "1f5abfe5-3406-4588-f992-ff411cbb8565" 1573 | }, 1574 | "execution_count": 79, 1575 | "outputs": [ 1576 | { 1577 | "output_type": "stream", 1578 | "name": "stdout", 1579 | "text": [ 1580 | "6\n", 1581 | "5\n", 1582 | "4\n" 1583 | ] 1584 | } 1585 | ] 1586 | }, 1587 | { 1588 | "cell_type": "code", 1589 | "metadata": { 1590 | "id": "mKiiYF0iGz5B", 1591 | "colab": { 1592 | "base_uri": "https://localhost:8080/" 1593 | }, 1594 | "outputId": "7017721a-59b1-44db-ab3f-e4b50b8d8870" 1595 | }, 1596 | "source": [ 1597 | "# Operaciones en ciclos\n", 1598 | "for i in range(3):\n", 1599 | " i+=1 \n", 1600 | "print('The loop is now done and the result is: ', i) " 1601 | ], 1602 | "execution_count": 80, 1603 | "outputs": [ 1604 | { 1605 | "output_type": "stream", 1606 | "name": "stdout", 1607 | "text": [ 1608 | "The loop is now done and the result is: 3\n" 1609 | ] 1610 | } 1611 | ] 1612 | }, 1613 | { 1614 | "cell_type": "code", 1615 | "metadata": { 1616 | "id": "qUTppZADlTM7", 1617 | "colab": { 1618 | "base_uri": "https://localhost:8080/" 1619 | }, 1620 | "outputId": "4c0fb0a6-07c2-47b7-e125-7f5d6b113221" 1621 | }, 1622 | "source": [ 1623 | "# En Python se usa el guión bajo (_) para variables cuyo valor no se necesita\n", 1624 | "for _ in range(10):\n", 1625 | " print('I do not care about i')" 1626 | ], 1627 | "execution_count": 81, 1628 | "outputs": [ 1629 | { 1630 | "output_type": "stream", 1631 | "name": "stdout", 1632 | "text": [ 1633 | "I do not care about i\n", 1634 | "I do not care about i\n", 1635 | "I do not care about i\n", 1636 | "I do not care about i\n", 1637 | "I do not care about i\n", 1638 | "I do not care about i\n", 1639 | "I do not care about i\n", 1640 | "I do not care about i\n", 1641 | "I do not care about i\n", 1642 | "I do not care about i\n" 1643 | ] 1644 | } 1645 | ] 1646 | }, 1647 | { 1648 | "cell_type": "code", 1649 | "source": [ 1650 | "#Ciclo while\n", 1651 | "a=1\n", 1652 | "while a <= 3:\n", 1653 | " print(a)\n", 1654 | " a += 1" 1655 | ], 1656 | "metadata": { 1657 | "colab": { 1658 | "base_uri": "https://localhost:8080/" 1659 | }, 1660 | "id": "OeTsNlGlm2OW", 1661 | "outputId": "0b7223f7-ed97-43dc-bde9-a7b797b024bb" 1662 | }, 1663 | "execution_count": 82, 1664 | "outputs": [ 1665 | { 1666 | "output_type": "stream", 1667 | "name": "stdout", 1668 | "text": [ 1669 | "1\n", 1670 | "2\n", 1671 | "3\n" 1672 | ] 1673 | } 1674 | ] 1675 | }, 1676 | { 1677 | "cell_type": "code", 1678 | "source": [ 1679 | "a" 1680 | ], 1681 | "metadata": { 1682 | "colab": { 1683 | "base_uri": "https://localhost:8080/" 1684 | }, 1685 | "id": "seHXGHQ_AbwO", 1686 | "outputId": "cdc8b563-17f5-48bc-88fc-89a53352bfa2" 1687 | }, 1688 | "execution_count": 83, 1689 | "outputs": [ 1690 | { 1691 | "output_type": "execute_result", 1692 | "data": { 1693 | "text/plain": [ 1694 | "4" 1695 | ] 1696 | }, 1697 | "metadata": {}, 1698 | "execution_count": 83 1699 | } 1700 | ] 1701 | }, 1702 | { 1703 | "cell_type": "markdown", 1704 | "metadata": { 1705 | "id": "qlAlR4AzGz5O" 1706 | }, 1707 | "source": [ 1708 | "# Sección 5. Condicionales" 1709 | ] 1710 | }, 1711 | { 1712 | "cell_type": "code", 1713 | "metadata": { 1714 | "id": "KVtghtsQGz5P", 1715 | "colab": { 1716 | "base_uri": "https://localhost:8080/" 1717 | }, 1718 | "outputId": "2fe629e0-9f4f-4f8b-84a5-e77151651cec" 1719 | }, 1720 | "source": [ 1721 | "# Ejemplos de \"if\", \"else\":\n", 1722 | "from random import randint \n", 1723 | "num = randint(1,10)\n", 1724 | "if num < 6:\n", 1725 | " print('The number', num, 'is less than 6')\n", 1726 | "else: \n", 1727 | " print('The number', num, 'is at least 6')" 1728 | ], 1729 | "execution_count": 97, 1730 | "outputs": [ 1731 | { 1732 | "output_type": "stream", 1733 | "name": "stdout", 1734 | "text": [ 1735 | "The number 9 is at least 6\n" 1736 | ] 1737 | } 1738 | ] 1739 | }, 1740 | { 1741 | "cell_type": "code", 1742 | "metadata": { 1743 | "id": "TriG-TwkoPhE", 1744 | "colab": { 1745 | "base_uri": "https://localhost:8080/" 1746 | }, 1747 | "outputId": "7afb1cae-ebb6-4e69-f75a-50576cd7bd27" 1748 | }, 1749 | "source": [ 1750 | "# Podemos no usar el \"else\" si solo nos interesa ejecutar una posibilidad de la condición:\n", 1751 | "\n", 1752 | "x = randint(1, 7)\n", 1753 | "y = randint(1, 7)\n", 1754 | "if x == y:\n", 1755 | " print('Threw doubles') \n", 1756 | "\n", 1757 | "print('This call to print will always be executed')" 1758 | ], 1759 | "execution_count": 102, 1760 | "outputs": [ 1761 | { 1762 | "output_type": "stream", 1763 | "name": "stdout", 1764 | "text": [ 1765 | "Threw doubles\n", 1766 | "This call to print will always be executed\n" 1767 | ] 1768 | } 1769 | ] 1770 | }, 1771 | { 1772 | "cell_type": "code", 1773 | "source": [ 1774 | "y" 1775 | ], 1776 | "metadata": { 1777 | "colab": { 1778 | "base_uri": "https://localhost:8080/" 1779 | }, 1780 | "id": "yj64WqHKB2aP", 1781 | "outputId": "30296618-d2d9-4a94-ce4e-c34963c60bec" 1782 | }, 1783 | "execution_count": 104, 1784 | "outputs": [ 1785 | { 1786 | "output_type": "execute_result", 1787 | "data": { 1788 | "text/plain": [ 1789 | "7" 1790 | ] 1791 | }, 1792 | "metadata": {}, 1793 | "execution_count": 104 1794 | } 1795 | ] 1796 | }, 1797 | { 1798 | "cell_type": "code", 1799 | "metadata": { 1800 | "id": "hvj7ei_vo-w5", 1801 | "colab": { 1802 | "base_uri": "https://localhost:8080/" 1803 | }, 1804 | "outputId": "6c067937-7872-4ffd-a50b-97e4017bf195" 1805 | }, 1806 | "source": [ 1807 | "# La opción \"elif\" permite verificar otras condiciones alternativas: \n", 1808 | "\n", 1809 | "x = randint(1, 7)\n", 1810 | "y = randint(1, 7)\n", 1811 | "if x == y:\n", 1812 | " print(x,'=',y,'Threw doubles')\n", 1813 | "elif x < y:\n", 1814 | " print(x, 'is less than', y)\n", 1815 | "elif x > y: # esto se puede reemplazar por \"else\" porque \"x > y\" es la única otra condición que falta por poner\n", 1816 | " print(x, 'is larger than', y)" 1817 | ], 1818 | "execution_count": 105, 1819 | "outputs": [ 1820 | { 1821 | "output_type": "stream", 1822 | "name": "stdout", 1823 | "text": [ 1824 | "1 is less than 5\n" 1825 | ] 1826 | } 1827 | ] 1828 | }, 1829 | { 1830 | "cell_type": "code", 1831 | "metadata": { 1832 | "id": "dqv9C-bPpZcd" 1833 | }, 1834 | "source": [ 1835 | "# Múltiples condiciones pueden combinarse usando 'and' y 'or'\n", 1836 | "\n", 1837 | "x = randint(1, 7)\n", 1838 | "y = randint(1, 7)\n", 1839 | "if x == y and x == 6 and y == 6: # El \"and y == 6\" puede omitirse porque viene implícito dentro de la condición \"x==y\".\n", 1840 | " print('Threw doubles of six')" 1841 | ], 1842 | "execution_count": 166, 1843 | "outputs": [] 1844 | }, 1845 | { 1846 | "cell_type": "code", 1847 | "metadata": { 1848 | "id": "f7p2RPF2pll-", 1849 | "colab": { 1850 | "base_uri": "https://localhost:8080/" 1851 | }, 1852 | "outputId": "178809f7-1476-439f-f3ed-b69764e71fa7" 1853 | }, 1854 | "source": [ 1855 | "x = 3\n", 1856 | "y = 10\n", 1857 | "if x < 5 or y < 5:\n", 1858 | " print('At least one number is less than 5')" 1859 | ], 1860 | "execution_count": 169, 1861 | "outputs": [ 1862 | { 1863 | "output_type": "stream", 1864 | "name": "stdout", 1865 | "text": [ 1866 | "At least one number is less than 5\n" 1867 | ] 1868 | } 1869 | ] 1870 | }, 1871 | { 1872 | "cell_type": "code", 1873 | "metadata": { 1874 | "id": "bTONpzJjp9Jk", 1875 | "colab": { 1876 | "base_uri": "https://localhost:8080/" 1877 | }, 1878 | "outputId": "1cc6cf09-c5bf-41e6-d7ab-5f8600e75c02" 1879 | }, 1880 | "source": [ 1881 | "# El operador \"or\" significa \"al menos una de las condiciones es verdadera\".\n", 1882 | "# Si se cumplen las dos, devuelve Verdadero (True). \n", 1883 | "x = 3\n", 1884 | "y = 3\n", 1885 | "if x < 5 or y < 5:\n", 1886 | " print('At least one number is less than 5')" 1887 | ], 1888 | "execution_count": 170, 1889 | "outputs": [ 1890 | { 1891 | "output_type": "stream", 1892 | "name": "stdout", 1893 | "text": [ 1894 | "At least one number is less than 5\n" 1895 | ] 1896 | } 1897 | ] 1898 | }, 1899 | { 1900 | "cell_type": "code", 1901 | "metadata": { 1902 | "id": "-TN1p3Z5qL0c", 1903 | "colab": { 1904 | "base_uri": "https://localhost:8080/" 1905 | }, 1906 | "outputId": "0d363ecf-55e4-4f80-ad1b-7b26a6b8926e" 1907 | }, 1908 | "source": [ 1909 | "# Cuando tenemos varios condicionales es mejor usar paréntesis para el orden:\n", 1910 | "x = 3\n", 1911 | "y = 5\n", 1912 | "if (x > 2) and (x + y == 8 or x < y):\n", 1913 | " print('Branch has been executed')" 1914 | ], 1915 | "execution_count": 174, 1916 | "outputs": [ 1917 | { 1918 | "output_type": "stream", 1919 | "name": "stdout", 1920 | "text": [ 1921 | "Branch has been executed\n" 1922 | ] 1923 | } 1924 | ] 1925 | }, 1926 | { 1927 | "cell_type": "markdown", 1928 | "metadata": { 1929 | "id": "lMjFC4wiq0ot" 1930 | }, 1931 | "source": [ 1932 | "### Resumen de posibles operadores condicionales\n", 1933 | "- if x>3: \t\t\tif x is greater than 3 \n", 1934 | "- if x>=3: \t\t\tif x is greater than or equal to 3 \n", 1935 | "- if x==3: \t\t\tif x is 3 \n", 1936 | "- if x!=3: \t\t\tif x is not 3 \n", 1937 | "- if x==3 and y==5:\tif x is 3 and y is 5\n", 1938 | "- if x==3 or y==5:\t\tif x is 3 or y is 5\n" 1939 | ] 1940 | }, 1941 | { 1942 | "cell_type": "markdown", 1943 | "metadata": { 1944 | "id": "B6JJTQMJiqSQ" 1945 | }, 1946 | "source": [ 1947 | "### Expresiones \"if\"" 1948 | ] 1949 | }, 1950 | { 1951 | "cell_type": "code", 1952 | "metadata": { 1953 | "id": "jRQX11Tcis2u", 1954 | "colab": { 1955 | "base_uri": "https://localhost:8080/" 1956 | }, 1957 | "outputId": "4ff825c7-a0c6-4598-8cda-35d0069384f6" 1958 | }, 1959 | "source": [ 1960 | "x = 3 #Prueba con 6\n", 1961 | "if x < 5:\n", 1962 | " name = 'Alice'\n", 1963 | "else:\n", 1964 | " name = 'Bob'\n", 1965 | "\n", 1966 | "print(name)" 1967 | ], 1968 | "execution_count": 176, 1969 | "outputs": [ 1970 | { 1971 | "output_type": "stream", 1972 | "name": "stdout", 1973 | "text": [ 1974 | "Bob\n" 1975 | ] 1976 | } 1977 | ] 1978 | }, 1979 | { 1980 | "cell_type": "code", 1981 | "metadata": { 1982 | "id": "4Qh8aTHajESu", 1983 | "colab": { 1984 | "base_uri": "https://localhost:8080/" 1985 | }, 1986 | "outputId": "dc40ba43-67d6-46ed-be3f-926eab8f3528" 1987 | }, 1988 | "source": [ 1989 | "# También se puede escribir en una línea\n", 1990 | "\n", 1991 | "x = 3\n", 1992 | "name = 'Alice' if x < 5 else 'Bob'\n", 1993 | "\n", 1994 | "print(name)" 1995 | ], 1996 | "execution_count": 178, 1997 | "outputs": [ 1998 | { 1999 | "output_type": "stream", 2000 | "name": "stdout", 2001 | "text": [ 2002 | "Bob\n" 2003 | ] 2004 | } 2005 | ] 2006 | }, 2007 | { 2008 | "cell_type": "code", 2009 | "source": [ 2010 | "#Definir una función\n", 2011 | "def greet(hour):\n", 2012 | " if hour < 12:\n", 2013 | " print('Good morning!')\n", 2014 | " elif hour >= 12 and hour < 20:\n", 2015 | " print('Good afternoon!')\n", 2016 | " else:\n", 2017 | " print('Good evening!')" 2018 | ], 2019 | "metadata": { 2020 | "id": "PCJ9iQf8nOsH" 2021 | }, 2022 | "execution_count": 179, 2023 | "outputs": [] 2024 | }, 2025 | { 2026 | "cell_type": "code", 2027 | "source": [ 2028 | "greet(5)" 2029 | ], 2030 | "metadata": { 2031 | "colab": { 2032 | "base_uri": "https://localhost:8080/" 2033 | }, 2034 | "id": "7HFnU02inSDc", 2035 | "outputId": "97ae787f-47c8-41e2-9157-a6beac90078f" 2036 | }, 2037 | "execution_count": 182, 2038 | "outputs": [ 2039 | { 2040 | "output_type": "stream", 2041 | "name": "stdout", 2042 | "text": [ 2043 | "Good evening!\n" 2044 | ] 2045 | } 2046 | ] 2047 | }, 2048 | { 2049 | "cell_type": "markdown", 2050 | "metadata": { 2051 | "id": "qjdejO_gGz5G" 2052 | }, 2053 | "source": [ 2054 | "# Sección 6. Números aleatorios" 2055 | ] 2056 | }, 2057 | { 2058 | "cell_type": "code", 2059 | "metadata": { 2060 | "id": "eg0h3xTtGz5J", 2061 | "colab": { 2062 | "base_uri": "https://localhost:8080/" 2063 | }, 2064 | "outputId": "3a42b035-21d5-4748-c63e-1a0939d08d4a" 2065 | }, 2066 | "source": [ 2067 | "# La funcion randint del paquete random se puede usar para generar enteros \n", 2068 | "# aleatorios dentro de un intervalo de valores. Los valores extremos del \n", 2069 | "# intervalos también se incluyen dentro de las posibilidades:\n", 2070 | "from random import randint\n", 2071 | "print(randint(-5, 6))" 2072 | ], 2073 | "execution_count": 186, 2074 | "outputs": [ 2075 | { 2076 | "output_type": "stream", 2077 | "name": "stdout", 2078 | "text": [ 2079 | "-4\n" 2080 | ] 2081 | } 2082 | ] 2083 | }, 2084 | { 2085 | "cell_type": "code", 2086 | "metadata": { 2087 | "id": "sSZWblYyfQU1", 2088 | "colab": { 2089 | "base_uri": "https://localhost:8080/" 2090 | }, 2091 | "outputId": "97e6264f-45df-4c39-dcff-8790afbba8ff" 2092 | }, 2093 | "source": [ 2094 | "# Número aleatorios de tipo float (flotantes) en un intervalo. En este caso el\n", 2095 | "# extremo superior no se incluye, es un intervalo abierto: \n", 2096 | "# [3, 4): el número 4 no está incluido.\n", 2097 | "from random import uniform\n", 2098 | "uniform(3, 4)" 2099 | ], 2100 | "execution_count": 193, 2101 | "outputs": [ 2102 | { 2103 | "output_type": "execute_result", 2104 | "data": { 2105 | "text/plain": [ 2106 | "3.482543194353482" 2107 | ] 2108 | }, 2109 | "metadata": {}, 2110 | "execution_count": 193 2111 | } 2112 | ] 2113 | }, 2114 | { 2115 | "cell_type": "markdown", 2116 | "metadata": { 2117 | "id": "QFbBAmrprauz" 2118 | }, 2119 | "source": [ 2120 | "# Sección 7. Funciones matemáticas" 2121 | ] 2122 | }, 2123 | { 2124 | "cell_type": "code", 2125 | "source": [ 2126 | "from math import factorial, sqrt, floor, ceil, log" 2127 | ], 2128 | "metadata": { 2129 | "id": "jakwDglKmQid" 2130 | }, 2131 | "execution_count": 195, 2132 | "outputs": [] 2133 | }, 2134 | { 2135 | "cell_type": "code", 2136 | "metadata": { 2137 | "id": "e_9Mbs1-rngr", 2138 | "colab": { 2139 | "base_uri": "https://localhost:8080/" 2140 | }, 2141 | "outputId": "a21f1be1-d412-4a4f-d47b-0bddf70c377d" 2142 | }, 2143 | "source": [ 2144 | "#Logaritmo natural (base e)\n", 2145 | "log(8)" 2146 | ], 2147 | "execution_count": 196, 2148 | "outputs": [ 2149 | { 2150 | "output_type": "execute_result", 2151 | "data": { 2152 | "text/plain": [ 2153 | "2.0794415416798357" 2154 | ] 2155 | }, 2156 | "metadata": {}, 2157 | "execution_count": 196 2158 | } 2159 | ] 2160 | }, 2161 | { 2162 | "cell_type": "code", 2163 | "metadata": { 2164 | "id": "17HzxRb2r0X9", 2165 | "colab": { 2166 | "base_uri": "https://localhost:8080/" 2167 | }, 2168 | "outputId": "ee93ed1a-1910-4203-8a33-7d1b35574d3f" 2169 | }, 2170 | "source": [ 2171 | "#Base 2\n", 2172 | "log(8, 2)" 2173 | ], 2174 | "execution_count": 197, 2175 | "outputs": [ 2176 | { 2177 | "output_type": "execute_result", 2178 | "data": { 2179 | "text/plain": [ 2180 | "3.0" 2181 | ] 2182 | }, 2183 | "metadata": {}, 2184 | "execution_count": 197 2185 | } 2186 | ] 2187 | }, 2188 | { 2189 | "cell_type": "code", 2190 | "metadata": { 2191 | "id": "wTTJK5tRr2hF", 2192 | "colab": { 2193 | "base_uri": "https://localhost:8080/" 2194 | }, 2195 | "outputId": "a0eb9c8d-84c1-4df0-c16a-ac298ddc5f1b" 2196 | }, 2197 | "source": [ 2198 | "factorial(4)" 2199 | ], 2200 | "execution_count": 198, 2201 | "outputs": [ 2202 | { 2203 | "output_type": "execute_result", 2204 | "data": { 2205 | "text/plain": [ 2206 | "24" 2207 | ] 2208 | }, 2209 | "metadata": {}, 2210 | "execution_count": 198 2211 | } 2212 | ] 2213 | }, 2214 | { 2215 | "cell_type": "code", 2216 | "metadata": { 2217 | "id": "tqn7BQQ_r5Dl", 2218 | "colab": { 2219 | "base_uri": "https://localhost:8080/" 2220 | }, 2221 | "outputId": "b951a2b2-8fa1-4d1b-cc6c-e807ff3569ff" 2222 | }, 2223 | "source": [ 2224 | "ceil(3.2)" 2225 | ], 2226 | "execution_count": 199, 2227 | "outputs": [ 2228 | { 2229 | "output_type": "execute_result", 2230 | "data": { 2231 | "text/plain": [ 2232 | "4" 2233 | ] 2234 | }, 2235 | "metadata": {}, 2236 | "execution_count": 199 2237 | } 2238 | ] 2239 | }, 2240 | { 2241 | "cell_type": "code", 2242 | "metadata": { 2243 | "id": "lXl7rqL3r7Ct", 2244 | "colab": { 2245 | "base_uri": "https://localhost:8080/" 2246 | }, 2247 | "outputId": "98f6ef0b-59ef-41fc-e4ca-54ed3657f22e" 2248 | }, 2249 | "source": [ 2250 | "ceil(-3.1)" 2251 | ], 2252 | "execution_count": 200, 2253 | "outputs": [ 2254 | { 2255 | "output_type": "execute_result", 2256 | "data": { 2257 | "text/plain": [ 2258 | "-3" 2259 | ] 2260 | }, 2261 | "metadata": {}, 2262 | "execution_count": 200 2263 | } 2264 | ] 2265 | }, 2266 | { 2267 | "cell_type": "code", 2268 | "metadata": { 2269 | "id": "5XWfZjgFr8-f", 2270 | "colab": { 2271 | "base_uri": "https://localhost:8080/" 2272 | }, 2273 | "outputId": "72e79760-fb2d-43bf-8f33-737289f50963" 2274 | }, 2275 | "source": [ 2276 | "floor(3.6)" 2277 | ], 2278 | "execution_count": 201, 2279 | "outputs": [ 2280 | { 2281 | "output_type": "execute_result", 2282 | "data": { 2283 | "text/plain": [ 2284 | "3" 2285 | ] 2286 | }, 2287 | "metadata": {}, 2288 | "execution_count": 201 2289 | } 2290 | ] 2291 | }, 2292 | { 2293 | "cell_type": "code", 2294 | "metadata": { 2295 | "id": "ZIQbgIqkr-BW", 2296 | "colab": { 2297 | "base_uri": "https://localhost:8080/" 2298 | }, 2299 | "outputId": "e480ac05-6c41-46b3-c5ef-b8a3f58de5ed" 2300 | }, 2301 | "source": [ 2302 | "floor(-3.1)" 2303 | ], 2304 | "execution_count": 202, 2305 | "outputs": [ 2306 | { 2307 | "output_type": "execute_result", 2308 | "data": { 2309 | "text/plain": [ 2310 | "-4" 2311 | ] 2312 | }, 2313 | "metadata": {}, 2314 | "execution_count": 202 2315 | } 2316 | ] 2317 | }, 2318 | { 2319 | "cell_type": "code", 2320 | "metadata": { 2321 | "id": "3gCAmgH2sDqe", 2322 | "colab": { 2323 | "base_uri": "https://localhost:8080/" 2324 | }, 2325 | "outputId": "7082a493-4085-4b02-a922-561dd6371ea9" 2326 | }, 2327 | "source": [ 2328 | "round(3.1)" 2329 | ], 2330 | "execution_count": 203, 2331 | "outputs": [ 2332 | { 2333 | "output_type": "execute_result", 2334 | "data": { 2335 | "text/plain": [ 2336 | "3" 2337 | ] 2338 | }, 2339 | "metadata": {}, 2340 | "execution_count": 203 2341 | } 2342 | ] 2343 | }, 2344 | { 2345 | "cell_type": "code", 2346 | "metadata": { 2347 | "id": "pQiOdpcfsBf2", 2348 | "colab": { 2349 | "base_uri": "https://localhost:8080/" 2350 | }, 2351 | "outputId": "6abba3de-b91c-4661-cf6a-3fddf01bc6e3" 2352 | }, 2353 | "source": [ 2354 | "round(3.5)" 2355 | ], 2356 | "execution_count": 204, 2357 | "outputs": [ 2358 | { 2359 | "output_type": "execute_result", 2360 | "data": { 2361 | "text/plain": [ 2362 | "4" 2363 | ] 2364 | }, 2365 | "metadata": {}, 2366 | "execution_count": 204 2367 | } 2368 | ] 2369 | }, 2370 | { 2371 | "cell_type": "code", 2372 | "metadata": { 2373 | "id": "kpHrF3MDsExv", 2374 | "colab": { 2375 | "base_uri": "https://localhost:8080/" 2376 | }, 2377 | "outputId": "7c7bc539-173e-4794-e06a-04c7229a87a0" 2378 | }, 2379 | "source": [ 2380 | "round(3.7)" 2381 | ], 2382 | "execution_count": 205, 2383 | "outputs": [ 2384 | { 2385 | "output_type": "execute_result", 2386 | "data": { 2387 | "text/plain": [ 2388 | "4" 2389 | ] 2390 | }, 2391 | "metadata": {}, 2392 | "execution_count": 205 2393 | } 2394 | ] 2395 | }, 2396 | { 2397 | "cell_type": "code", 2398 | "source": [ 2399 | "abs(-3)" 2400 | ], 2401 | "metadata": { 2402 | "colab": { 2403 | "base_uri": "https://localhost:8080/" 2404 | }, 2405 | "id": "5xoyPWuFn3oC", 2406 | "outputId": "f9630d38-88d4-410d-df6f-b07286967e7a" 2407 | }, 2408 | "execution_count": 206, 2409 | "outputs": [ 2410 | { 2411 | "output_type": "execute_result", 2412 | "data": { 2413 | "text/plain": [ 2414 | "3" 2415 | ] 2416 | }, 2417 | "metadata": {}, 2418 | "execution_count": 206 2419 | } 2420 | ] 2421 | }, 2422 | { 2423 | "cell_type": "markdown", 2424 | "source": [ 2425 | "# Sección 8. Listas" 2426 | ], 2427 | "metadata": { 2428 | "id": "3boxpJ0Ashut" 2429 | } 2430 | }, 2431 | { 2432 | "cell_type": "code", 2433 | "source": [ 2434 | "print(mensaje)\n", 2435 | "print(type(mensaje))" 2436 | ], 2437 | "metadata": { 2438 | "colab": { 2439 | "base_uri": "https://localhost:8080/" 2440 | }, 2441 | "id": "dHmMExNpslE1", 2442 | "outputId": "54107c08-066b-4f48-95a9-64deb9101d5d" 2443 | }, 2444 | "execution_count": 208, 2445 | "outputs": [ 2446 | { 2447 | "output_type": "stream", 2448 | "name": "stdout", 2449 | "text": [ 2450 | "Soy alumno del Curso de Python de Eli\n", 2451 | "\n" 2452 | ] 2453 | } 2454 | ] 2455 | }, 2456 | { 2457 | "cell_type": "code", 2458 | "source": [ 2459 | "letters_mensaje=list(mensaje)\n", 2460 | "letters_mensaje" 2461 | ], 2462 | "metadata": { 2463 | "colab": { 2464 | "base_uri": "https://localhost:8080/" 2465 | }, 2466 | "id": "gAKl8keBtFOQ", 2467 | "outputId": "459bf0ac-bb10-496d-da80-e3138c93324a" 2468 | }, 2469 | "execution_count": 209, 2470 | "outputs": [ 2471 | { 2472 | "output_type": "execute_result", 2473 | "data": { 2474 | "text/plain": [ 2475 | "['S',\n", 2476 | " 'o',\n", 2477 | " 'y',\n", 2478 | " ' ',\n", 2479 | " 'a',\n", 2480 | " 'l',\n", 2481 | " 'u',\n", 2482 | " 'm',\n", 2483 | " 'n',\n", 2484 | " 'o',\n", 2485 | " ' ',\n", 2486 | " 'd',\n", 2487 | " 'e',\n", 2488 | " 'l',\n", 2489 | " ' ',\n", 2490 | " 'C',\n", 2491 | " 'u',\n", 2492 | " 'r',\n", 2493 | " 's',\n", 2494 | " 'o',\n", 2495 | " ' ',\n", 2496 | " 'd',\n", 2497 | " 'e',\n", 2498 | " ' ',\n", 2499 | " 'P',\n", 2500 | " 'y',\n", 2501 | " 't',\n", 2502 | " 'h',\n", 2503 | " 'o',\n", 2504 | " 'n',\n", 2505 | " ' ',\n", 2506 | " 'd',\n", 2507 | " 'e',\n", 2508 | " ' ',\n", 2509 | " 'E',\n", 2510 | " 'l',\n", 2511 | " 'i']" 2512 | ] 2513 | }, 2514 | "metadata": {}, 2515 | "execution_count": 209 2516 | } 2517 | ] 2518 | }, 2519 | { 2520 | "cell_type": "code", 2521 | "source": [ 2522 | "split_mensaje" 2523 | ], 2524 | "metadata": { 2525 | "colab": { 2526 | "base_uri": "https://localhost:8080/" 2527 | }, 2528 | "id": "_VTpzTvxtC-A", 2529 | "outputId": "f6c78375-85e9-4809-ac59-7c9db2159cda" 2530 | }, 2531 | "execution_count": 210, 2532 | "outputs": [ 2533 | { 2534 | "output_type": "execute_result", 2535 | "data": { 2536 | "text/plain": [ 2537 | "['Soy', 'alumno', 'del', 'Curso', 'de', 'Python']" 2538 | ] 2539 | }, 2540 | "metadata": {}, 2541 | "execution_count": 210 2542 | } 2543 | ] 2544 | }, 2545 | { 2546 | "cell_type": "code", 2547 | "source": [ 2548 | "print(split_mensaje)\n", 2549 | "print(type(split_mensaje))" 2550 | ], 2551 | "metadata": { 2552 | "colab": { 2553 | "base_uri": "https://localhost:8080/" 2554 | }, 2555 | "id": "oFnJcXt1svCR", 2556 | "outputId": "b00e5323-a040-4734-82ba-dfc9c0217148" 2557 | }, 2558 | "execution_count": 211, 2559 | "outputs": [ 2560 | { 2561 | "output_type": "stream", 2562 | "name": "stdout", 2563 | "text": [ 2564 | "['Soy', 'alumno', 'del', 'Curso', 'de', 'Python']\n", 2565 | "\n" 2566 | ] 2567 | } 2568 | ] 2569 | }, 2570 | { 2571 | "cell_type": "code", 2572 | "source": [ 2573 | "#Seleccion de los dos primeros elementos de la lista\n", 2574 | "split_mensaje[0:2]" 2575 | ], 2576 | "metadata": { 2577 | "colab": { 2578 | "base_uri": "https://localhost:8080/" 2579 | }, 2580 | "id": "YPaQR4BLtM6T", 2581 | "outputId": "4bb6fd36-2eb5-440d-d116-851b89911853" 2582 | }, 2583 | "execution_count": 212, 2584 | "outputs": [ 2585 | { 2586 | "output_type": "execute_result", 2587 | "data": { 2588 | "text/plain": [ 2589 | "['Soy', 'alumno']" 2590 | ] 2591 | }, 2592 | "metadata": {}, 2593 | "execution_count": 212 2594 | } 2595 | ] 2596 | }, 2597 | { 2598 | "cell_type": "code", 2599 | "source": [ 2600 | "#No modifica la lista original\n", 2601 | "list(reversed(split_mensaje))" 2602 | ], 2603 | "metadata": { 2604 | "colab": { 2605 | "base_uri": "https://localhost:8080/" 2606 | }, 2607 | "id": "N8qqRAbatZdN", 2608 | "outputId": "55b51c0c-03b9-4d26-b8e6-7555a7bee327" 2609 | }, 2610 | "execution_count": 213, 2611 | "outputs": [ 2612 | { 2613 | "output_type": "execute_result", 2614 | "data": { 2615 | "text/plain": [ 2616 | "['Python', 'de', 'Curso', 'del', 'alumno', 'Soy']" 2617 | ] 2618 | }, 2619 | "metadata": {}, 2620 | "execution_count": 213 2621 | } 2622 | ] 2623 | }, 2624 | { 2625 | "cell_type": "code", 2626 | "source": [ 2627 | "#Modifica la lista original\n", 2628 | "split_mensaje.reverse()" 2629 | ], 2630 | "metadata": { 2631 | "id": "7FguzjbXtniD" 2632 | }, 2633 | "execution_count": 214, 2634 | "outputs": [] 2635 | }, 2636 | { 2637 | "cell_type": "code", 2638 | "source": [ 2639 | "split_mensaje" 2640 | ], 2641 | "metadata": { 2642 | "colab": { 2643 | "base_uri": "https://localhost:8080/" 2644 | }, 2645 | "id": "ag1MrubJtrCh", 2646 | "outputId": "13ff3992-0823-48c4-af6c-f59fd271a0a4" 2647 | }, 2648 | "execution_count": 215, 2649 | "outputs": [ 2650 | { 2651 | "output_type": "execute_result", 2652 | "data": { 2653 | "text/plain": [ 2654 | "['Python', 'de', 'Curso', 'del', 'alumno', 'Soy']" 2655 | ] 2656 | }, 2657 | "metadata": {}, 2658 | "execution_count": 215 2659 | } 2660 | ] 2661 | }, 2662 | { 2663 | "cell_type": "code", 2664 | "source": [ 2665 | "split_mensaje.reverse()\n", 2666 | "split_mensaje" 2667 | ], 2668 | "metadata": { 2669 | "colab": { 2670 | "base_uri": "https://localhost:8080/" 2671 | }, 2672 | "id": "8wJ9JBK8t0qz", 2673 | "outputId": "9c553a84-054a-4007-e713-d7dc2e2e02ea" 2674 | }, 2675 | "execution_count": 216, 2676 | "outputs": [ 2677 | { 2678 | "output_type": "execute_result", 2679 | "data": { 2680 | "text/plain": [ 2681 | "['Soy', 'alumno', 'del', 'Curso', 'de', 'Python']" 2682 | ] 2683 | }, 2684 | "metadata": {}, 2685 | "execution_count": 216 2686 | } 2687 | ] 2688 | }, 2689 | { 2690 | "cell_type": "code", 2691 | "source": [ 2692 | "#Añadir valores (modifica la original)\n", 2693 | "split_mensaje.append('de')\n", 2694 | "split_mensaje.append('Eli')\n", 2695 | "split_mensaje" 2696 | ], 2697 | "metadata": { 2698 | "colab": { 2699 | "base_uri": "https://localhost:8080/" 2700 | }, 2701 | "id": "07QUI_WFt4Ms", 2702 | "outputId": "91e106ab-a511-44d8-fc6e-9c616acc6fc1" 2703 | }, 2704 | "execution_count": 217, 2705 | "outputs": [ 2706 | { 2707 | "output_type": "execute_result", 2708 | "data": { 2709 | "text/plain": [ 2710 | "['Soy', 'alumno', 'del', 'Curso', 'de', 'Python', 'de', 'Eli']" 2711 | ] 2712 | }, 2713 | "metadata": {}, 2714 | "execution_count": 217 2715 | } 2716 | ] 2717 | }, 2718 | { 2719 | "cell_type": "code", 2720 | "source": [ 2721 | "#Añadir valores (modifica la original)\n", 2722 | "split_mensaje.insert(2,'o')\n", 2723 | "split_mensaje.insert(3,'alumna')\n", 2724 | "split_mensaje" 2725 | ], 2726 | "metadata": { 2727 | "colab": { 2728 | "base_uri": "https://localhost:8080/" 2729 | }, 2730 | "id": "VpKGjqutuWiq", 2731 | "outputId": "d10bb366-93f7-403e-a0d6-4fdf4ef02621" 2732 | }, 2733 | "execution_count": 218, 2734 | "outputs": [ 2735 | { 2736 | "output_type": "execute_result", 2737 | "data": { 2738 | "text/plain": [ 2739 | "['Soy', 'alumno', 'o', 'alumna', 'del', 'Curso', 'de', 'Python', 'de', 'Eli']" 2740 | ] 2741 | }, 2742 | "metadata": {}, 2743 | "execution_count": 218 2744 | } 2745 | ] 2746 | }, 2747 | { 2748 | "cell_type": "code", 2749 | "source": [ 2750 | "#Modificar un elemento o varios\n", 2751 | "split_mensaje[0]='Somos'\n", 2752 | "split_mensaje" 2753 | ], 2754 | "metadata": { 2755 | "colab": { 2756 | "base_uri": "https://localhost:8080/" 2757 | }, 2758 | "id": "7fARXo_tutK_", 2759 | "outputId": "61421caf-9d84-45e0-c2db-e7001d8cdbb1" 2760 | }, 2761 | "execution_count": 219, 2762 | "outputs": [ 2763 | { 2764 | "output_type": "execute_result", 2765 | "data": { 2766 | "text/plain": [ 2767 | "['Somos', 'alumno', 'o', 'alumna', 'del', 'Curso', 'de', 'Python', 'de', 'Eli']" 2768 | ] 2769 | }, 2770 | "metadata": {}, 2771 | "execution_count": 219 2772 | } 2773 | ] 2774 | }, 2775 | { 2776 | "cell_type": "code", 2777 | "source": [ 2778 | "split_mensaje[1]='alumnos'\n", 2779 | "split_mensaje" 2780 | ], 2781 | "metadata": { 2782 | "colab": { 2783 | "base_uri": "https://localhost:8080/" 2784 | }, 2785 | "id": "vT02PxETuvNL", 2786 | "outputId": "219e6060-8b35-4198-b6b9-a288e418fcf1" 2787 | }, 2788 | "execution_count": 220, 2789 | "outputs": [ 2790 | { 2791 | "output_type": "execute_result", 2792 | "data": { 2793 | "text/plain": [ 2794 | "['Somos',\n", 2795 | " 'alumnos',\n", 2796 | " 'o',\n", 2797 | " 'alumna',\n", 2798 | " 'del',\n", 2799 | " 'Curso',\n", 2800 | " 'de',\n", 2801 | " 'Python',\n", 2802 | " 'de',\n", 2803 | " 'Eli']" 2804 | ] 2805 | }, 2806 | "metadata": {}, 2807 | "execution_count": 220 2808 | } 2809 | ] 2810 | }, 2811 | { 2812 | "cell_type": "code", 2813 | "source": [ 2814 | "#Borrar elementos\n", 2815 | "split_mensaje.remove('o')" 2816 | ], 2817 | "metadata": { 2818 | "id": "mkUvglDlvPGN" 2819 | }, 2820 | "execution_count": 221, 2821 | "outputs": [] 2822 | }, 2823 | { 2824 | "cell_type": "code", 2825 | "source": [ 2826 | "split_mensaje" 2827 | ], 2828 | "metadata": { 2829 | "colab": { 2830 | "base_uri": "https://localhost:8080/" 2831 | }, 2832 | "id": "u8Y-PiWLvWCl", 2833 | "outputId": "8cd366f3-c170-47da-dab5-22512f1db82e" 2834 | }, 2835 | "execution_count": 222, 2836 | "outputs": [ 2837 | { 2838 | "output_type": "execute_result", 2839 | "data": { 2840 | "text/plain": [ 2841 | "['Somos', 'alumnos', 'alumna', 'del', 'Curso', 'de', 'Python', 'de', 'Eli']" 2842 | ] 2843 | }, 2844 | "metadata": {}, 2845 | "execution_count": 222 2846 | } 2847 | ] 2848 | }, 2849 | { 2850 | "cell_type": "code", 2851 | "source": [ 2852 | "del(split_mensaje[2])\n", 2853 | "split_mensaje" 2854 | ], 2855 | "metadata": { 2856 | "colab": { 2857 | "base_uri": "https://localhost:8080/" 2858 | }, 2859 | "id": "d-jjC_SrvXdW", 2860 | "outputId": "b6219754-1de4-4b33-c3eb-d4f216f7ecfb" 2861 | }, 2862 | "execution_count": 223, 2863 | "outputs": [ 2864 | { 2865 | "output_type": "execute_result", 2866 | "data": { 2867 | "text/plain": [ 2868 | "['Somos', 'alumnos', 'del', 'Curso', 'de', 'Python', 'de', 'Eli']" 2869 | ] 2870 | }, 2871 | "metadata": {}, 2872 | "execution_count": 223 2873 | } 2874 | ] 2875 | }, 2876 | { 2877 | "cell_type": "code", 2878 | "source": [ 2879 | "#Indices\n", 2880 | "split_mensaje.index('Curso')" 2881 | ], 2882 | "metadata": { 2883 | "colab": { 2884 | "base_uri": "https://localhost:8080/" 2885 | }, 2886 | "id": "qTJJnqZevkOM", 2887 | "outputId": "b439badd-1e73-4a20-9a5a-586c2d9e06f7" 2888 | }, 2889 | "execution_count": 224, 2890 | "outputs": [ 2891 | { 2892 | "output_type": "execute_result", 2893 | "data": { 2894 | "text/plain": [ 2895 | "3" 2896 | ] 2897 | }, 2898 | "metadata": {}, 2899 | "execution_count": 224 2900 | } 2901 | ] 2902 | }, 2903 | { 2904 | "cell_type": "code", 2905 | "source": [ 2906 | "#Pertenencia de elementos\n", 2907 | "'Python' in split_mensaje" 2908 | ], 2909 | "metadata": { 2910 | "colab": { 2911 | "base_uri": "https://localhost:8080/" 2912 | }, 2913 | "id": "KPFJStVtvn7W", 2914 | "outputId": "81a312a5-99df-4356-eaf4-1452ecb53fde" 2915 | }, 2916 | "execution_count": 225, 2917 | "outputs": [ 2918 | { 2919 | "output_type": "execute_result", 2920 | "data": { 2921 | "text/plain": [ 2922 | "True" 2923 | ] 2924 | }, 2925 | "metadata": {}, 2926 | "execution_count": 225 2927 | } 2928 | ] 2929 | }, 2930 | { 2931 | "cell_type": "code", 2932 | "source": [ 2933 | "#Contador de elementos\n", 2934 | "split_mensaje.count('de')" 2935 | ], 2936 | "metadata": { 2937 | "colab": { 2938 | "base_uri": "https://localhost:8080/" 2939 | }, 2940 | "id": "08Hq7KiBvwFz", 2941 | "outputId": "5116cccc-711e-4bb5-fa8f-33b3d2954bcc" 2942 | }, 2943 | "execution_count": 226, 2944 | "outputs": [ 2945 | { 2946 | "output_type": "execute_result", 2947 | "data": { 2948 | "text/plain": [ 2949 | "2" 2950 | ] 2951 | }, 2952 | "metadata": {}, 2953 | "execution_count": 226 2954 | } 2955 | ] 2956 | }, 2957 | { 2958 | "cell_type": "code", 2959 | "source": [ 2960 | "#Convertir lista en cadena de texto (string)\n", 2961 | "string_split_mensaje=' '.join(split_mensaje)\n", 2962 | "string_split_mensaje" 2963 | ], 2964 | "metadata": { 2965 | "colab": { 2966 | "base_uri": "https://localhost:8080/", 2967 | "height": 35 2968 | }, 2969 | "id": "QfEL2adFv1UW", 2970 | "outputId": "b44eef6d-0620-46eb-af38-9acb93e46416" 2971 | }, 2972 | "execution_count": 230, 2973 | "outputs": [ 2974 | { 2975 | "output_type": "execute_result", 2976 | "data": { 2977 | "text/plain": [ 2978 | "'Somos alumnos del Curso de Python de Eli'" 2979 | ], 2980 | "application/vnd.google.colaboratory.intrinsic+json": { 2981 | "type": "string" 2982 | } 2983 | }, 2984 | "metadata": {}, 2985 | "execution_count": 230 2986 | } 2987 | ] 2988 | }, 2989 | { 2990 | "cell_type": "code", 2991 | "source": [ 2992 | "#Orden\n", 2993 | "sorted(split_mensaje)" 2994 | ], 2995 | "metadata": { 2996 | "colab": { 2997 | "base_uri": "https://localhost:8080/" 2998 | }, 2999 | "id": "DIYmMDSjwIMH", 3000 | "outputId": "e154da33-7124-475c-bce9-1365eb1d7eb6" 3001 | }, 3002 | "execution_count": 231, 3003 | "outputs": [ 3004 | { 3005 | "output_type": "execute_result", 3006 | "data": { 3007 | "text/plain": [ 3008 | "['Curso', 'Eli', 'Python', 'Somos', 'alumnos', 'de', 'de', 'del']" 3009 | ] 3010 | }, 3011 | "metadata": {}, 3012 | "execution_count": 231 3013 | } 3014 | ] 3015 | }, 3016 | { 3017 | "cell_type": "code", 3018 | "source": [ 3019 | "#Iterar\n", 3020 | "for word in split_mensaje:\n", 3021 | " print(word)" 3022 | ], 3023 | "metadata": { 3024 | "colab": { 3025 | "base_uri": "https://localhost:8080/" 3026 | }, 3027 | "id": "DM7vNtjmwgmM", 3028 | "outputId": "e2bce02b-bb54-4354-8e4d-bd38ed5efdff" 3029 | }, 3030 | "execution_count": 233, 3031 | "outputs": [ 3032 | { 3033 | "output_type": "stream", 3034 | "name": "stdout", 3035 | "text": [ 3036 | "Somos\n", 3037 | "alumnos\n", 3038 | "del\n", 3039 | "Curso\n", 3040 | "de\n", 3041 | "Python\n", 3042 | "de\n", 3043 | "Eli\n" 3044 | ] 3045 | } 3046 | ] 3047 | }, 3048 | { 3049 | "cell_type": "code", 3050 | "source": [ 3051 | "#Iterar\n", 3052 | "for i, word in enumerate(split_mensaje):\n", 3053 | " print(i, word) " 3054 | ], 3055 | "metadata": { 3056 | "colab": { 3057 | "base_uri": "https://localhost:8080/" 3058 | }, 3059 | "id": "3fyB5ruUyXBB", 3060 | "outputId": "98de6c25-a93d-494e-f23e-356d78d855ec" 3061 | }, 3062 | "execution_count": 234, 3063 | "outputs": [ 3064 | { 3065 | "output_type": "stream", 3066 | "name": "stdout", 3067 | "text": [ 3068 | "0 Somos\n", 3069 | "1 alumnos\n", 3070 | "2 del\n", 3071 | "3 Curso\n", 3072 | "4 de\n", 3073 | "5 Python\n", 3074 | "6 de\n", 3075 | "7 Eli\n" 3076 | ] 3077 | } 3078 | ] 3079 | } 3080 | ] 3081 | } -------------------------------------------------------------------------------- /10 Visualizaciones avanzadas con ggplot2 en R/Visualizacion avanzada ggplot2.R: -------------------------------------------------------------------------------- 1 | ### Visualizaciones avanzadas con ggplot2 2 | 3 | # Instalando el paquete 4 | install.packages("ggplot2") 5 | library(ggplot2) 6 | 7 | # Importar los datos 8 | # Dataset "mpg" (pertenece a ggplot2) 9 | ?mpg 10 | data <- ggplot2::mpg 11 | 12 | # Convertir las variables categóricas en tipo "factor" 13 | data <- transform(data, 14 | cyl = factor(cyl), 15 | drv = factor(drv), 16 | fl = factor(fl), 17 | year = factor(year), 18 | class = factor(class) 19 | ) 20 | 21 | ## Scatterplot (Diagrama de dispersion) 22 | ggplot(data) + # datos 23 | aes(x=displ, y=hwy) + # variables 24 | geom_point() #tipo de gráfico: puntos 25 | 26 | # Forma alternativa (con el aes dentro del ggplot): 27 | ggplot(data, aes(x=displ, y=hwy) ) + # datos y variables 28 | geom_point() #tipo de gráfico: puntos 29 | 30 | ## Gráfico de línea 31 | ggplot(data) + 32 | aes(x = displ, y = hwy) + 33 | geom_line() #tipo de gráfico: linea 34 | 35 | ## Gráfico combinado de líneas y puntos 36 | ggplot(data) + 37 | aes(x = displ, y = hwy) + 38 | geom_point() + 39 | geom_line() # añadir linea 40 | 41 | # Cambiar colores y tamaño 42 | ggplot(data) + 43 | aes(x = displ, y = hwy, color="red", size=cty) + 44 | geom_point() 45 | 46 | # color respecto a una variable categorica y tamaño con cuantitativa 47 | ggplot(data) + 48 | aes(x = displ, y = hwy, color=year, size=cty) + 49 | geom_point() 50 | 51 | # color respecto a una variable cuantitativa 52 | ggplot(data) + 53 | aes(x = displ, y = hwy, color = cty) + 54 | geom_point() + 55 | scale_colour_gradient2( 56 | low = "green", 57 | mid = "gray", 58 | high = "red", 59 | midpoint = median(data$cty) 60 | ) 61 | 62 | # añadir texto en puntos 63 | ggplot(data) + 64 | aes(x = displ, y = hwy, color = cty) + 65 | geom_point() + 66 | geom_text(aes(label = rownames(data)), 67 | check_overlap = TRUE, 68 | size = 2, 69 | vjust = -1) 70 | 71 | #añadir texto en grafico 72 | ggplot(data) + 73 | aes(x = displ, y = hwy, color = cty) + 74 | geom_point() + 75 | annotate("text", 76 | x = 5, 77 | y = 40, 78 | label = "hwy and displ are \n negatively correlated \n (rho = -0.77, p-value < 0.001)", 79 | size = 3 80 | ) 81 | 82 | # suavizado (smooth) 83 | ggplot(data) + 84 | aes(x = displ, y = hwy, color = cty) + 85 | geom_point() + 86 | geom_smooth() 87 | 88 | # metodo lm (regresión lineal) 89 | ggplot(data) + 90 | aes(x = displ, y = hwy, color = cty) + 91 | geom_point()+ 92 | geom_smooth(method = lm) 93 | 94 | # facetas: dividir en paneles 95 | ggplot(data) + 96 | aes(x = displ, y = hwy, color = cty) + 97 | geom_point() + 98 | facet_grid(. ~ drv) 99 | 100 | 101 | ## Histograma 102 | ggplot(data) + 103 | aes(x = hwy) + 104 | geom_histogram() 105 | 106 | # Numero de barras = sqrt(n) 107 | ggplot(data) + 108 | aes(x = hwy) + 109 | geom_histogram(bins = sqrt(nrow(data))) 110 | 111 | 112 | # Personalizacion 113 | ggplot(data) + 114 | aes(x = hwy) + 115 | geom_histogram(bins = sqrt(nrow(data)), fill="magenta") # fill con color 116 | 117 | ggplot(data) + 118 | aes(x = hwy) + 119 | geom_histogram(bins = sqrt(nrow(data)), aes(fill=class)) # fill con variable 120 | 121 | ggplot(data) + 122 | aes(x = hwy) + 123 | geom_histogram(bins = sqrt(nrow(data)), aes(fill=class), color="black") # color del borde 124 | 125 | 126 | ## Gráfico de densidades 127 | ggplot(data) + 128 | aes(x = hwy) + 129 | geom_density() 130 | 131 | # varias 132 | ggplot(data) + 133 | aes(x = hwy) + 134 | geom_density(aes(fill=drv), alpha=0.25) 135 | 136 | ## Combinacion de histograma + densidad 137 | ggplot(data) + 138 | aes(x = hwy, y = ..density..) + 139 | geom_histogram() + 140 | geom_density() 141 | 142 | 143 | ## Boxplot 144 | ggplot(data) + 145 | aes(x = "", y = hwy) + 146 | geom_boxplot() 147 | 148 | # varios 149 | ggplot(data) + 150 | aes(x = drv, y = hwy) + 151 | geom_boxplot() 152 | 153 | 154 | ggplot(data) + 155 | aes(x = drv, y = hwy) + 156 | geom_boxplot() + 157 | geom_jitter(alpha = 0.25, width = 0.2) # datos aleatorios 158 | 159 | 160 | ggplot(data) + 161 | aes(x = drv, y = hwy) + 162 | geom_boxplot() + # 163 | geom_jitter(alpha = 0.25, width = 0.2) + 164 | facet_wrap(~year) # faceta: dividir en 2 paneles según el año 165 | 166 | 167 | 168 | ggplot(data) + 169 | aes(x = drv, y = hwy, fill = drv) + # añadir color de relleno 170 | geom_boxplot(varwidth = TRUE) + 171 | geom_jitter(alpha = 0.25, width = 0.2) + 172 | facet_wrap(~year) 173 | 174 | ## Barplot 175 | ggplot(data) + 176 | aes(x = drv) + 177 | geom_bar() 178 | 179 | # con colores 180 | ggplot(data) + 181 | aes(x = drv, fill = drv) + # colores de relleno 182 | geom_bar() + 183 | theme(legend.position = "none") # quitar leyenda 184 | 185 | 186 | ggplot(data) + 187 | aes(x = drv, fill = year) + # fill con respecto a una vble cualitativa: years 188 | geom_bar() 189 | 190 | # para comparar entre grupos (proporciones), se pueden poner las barras del mismo tamaño 191 | ggplot(data) + 192 | geom_bar(aes(x = drv, fill = year), position = "fill") #position fill 193 | 194 | # dibujar dos barras una a continuacion de la otra para cada valor de drv 195 | ggplot(data) + 196 | geom_bar(aes(x = drv, fill = year), position = "dodge") 197 | 198 | 199 | ## Personalizacion avanzada 200 | # Titulos, subtitulos, etc 201 | p <- ggplot(data) + 202 | aes(x = displ, y = hwy) + 203 | geom_point() 204 | p + labs( 205 | title = "Fuel efficiency for 38 popular models of car", 206 | subtitle = "Period 1999-2008", 207 | caption = "Data: ggplot2::mpg. See more at www.statsandr.com", 208 | x = "Engine displacement (litres)", 209 | y = "Highway miles per gallon (mpg)" 210 | ) 211 | 212 | 213 | # Temas: modificar aún más los labs: 214 | p + labs( 215 | title = "Fuel efficiency for 38 popular models of car", 216 | subtitle = "Period 1999-2008", 217 | caption = "Data: ggplot2::mpg. See more at www.statsandr.com", 218 | x = "Engine displacement (litres)", 219 | y = "Highway miles per gallon (mpg)" 220 | ) + 221 | theme( 222 | plot.title = element_text( 223 | hjust = 0.5, # center 224 | size = 12, 225 | color = "steelblue", 226 | face = "bold" 227 | ), 228 | plot.subtitle = element_text( 229 | hjust = 0.5, # center 230 | size = 10, 231 | color = "gray", 232 | face = "italic" 233 | ) 234 | ) 235 | 236 | 237 | # Leyenda 238 | p + aes(color = class) + 239 | theme(legend.position = "top") 240 | 241 | 242 | # Creando varios gráficos de diferentes tipos en una sola imagen 243 | p_a <- ggplot(data) + 244 | aes(x = displ, y = hwy) + 245 | geom_point() 246 | p_b <- ggplot(data) + 247 | aes(x = hwy) + 248 | geom_histogram() 249 | p_c <- ggplot(data) + 250 | aes(x = drv, y = hwy) + 251 | geom_boxplot() 252 | 253 | install.packages("patchwork") 254 | library(patchwork) 255 | 256 | p_a + p_b + p_c 257 | 258 | p_a / p_b / p_c 259 | 260 | p_a + p_b / p_c 261 | 262 | (p_a + p_b) / p_c 263 | 264 | 265 | # Ajustando las coordenadas 266 | # sin ajustar 267 | p1 <- ggplot(data) + 268 | aes(x = class, y = hwy) + 269 | geom_boxplot() 270 | # ajustadas 271 | p2 <- ggplot(data) + 272 | aes(x = class, y = hwy) + 273 | geom_boxplot() + 274 | coord_flip() 275 | 276 | 277 | p1 + p2 278 | 279 | # vertical 280 | ggplot(data) + 281 | aes(x = class) + 282 | geom_bar() 283 | 284 | # horizontal 285 | ggplot(data) + 286 | aes(x = class) + 287 | geom_bar() + 288 | coord_flip() 289 | 290 | # Guardar gráfico 291 | getwd() 292 | setwd('/Users/elisacabana/Documents/UDEMY Github/0. Curso R y Python/11 Librería ggplot2') 293 | getwd() 294 | ggplot(data) + 295 | aes(x = displ, y = hwy) + 296 | geom_point() 297 | ggsave("plot1.pdf") 298 | getwd() 299 | 300 | 301 | 302 | -------------------------------------------------------------------------------- /11 Series temporales en R/Series temporales con R.R: -------------------------------------------------------------------------------- 1 | # Series temporales en R 2 | 3 | library(readr) 4 | sbux.df <- read.csv("~/Documents/UDEMY Github/0. Curso R y Python/12 Series temporales en R/2 Trabajar con series temporales en R/sbuxPrices.csv") 5 | 6 | msft.df <- read.csv("~/Documents/UDEMY Github/0. Curso R y Python/12 Series temporales en R/2 Trabajar con series temporales en R/msftPrices.csv") 7 | 8 | 9 | # Convertir los datos en time-series 10 | sbux.ts = ts(data=sbux.df$Adj.Close, frequency = 12, start=c(1993,3), end=c(2008,3)) 11 | class(sbux.ts) #clase ts (time series) 12 | 13 | msft.ts = ts(data=msft.df$Adj.Close, frequency = 12, start=c(1993,3), end=c(2008,3)) 14 | class(msft.ts) 15 | 16 | # Fechas y frecuencia de la serie 17 | sbux.ts 18 | start(sbux.ts) 19 | end(sbux.ts) 20 | frequency(sbux.ts) 21 | 22 | # Extraer un subconjunto de la serie 23 | tmp = sbux.ts[1:5] #numeric 24 | class(tmp) 25 | 26 | tmp = window(sbux.ts, start=c(1993, 3), end=c(1993,8)) # se mantiene como "ts" 27 | class(tmp) 28 | 29 | 30 | # Combinar dos series temporales en dos columnas dentro del mismo dataset 31 | sbuxmsft.ts = cbind(sbux.ts, msft.ts) 32 | class(sbuxmsft.ts) #clase mts (multiple time series) 33 | 34 | 35 | # Plot objeto ts 36 | plot(sbux.ts, col="blue", lwd=2, ylab="Adjusted close", 37 | main="Monthly closing price of SBUX") 38 | 39 | 40 | 41 | # Dibujar un subconjunto (Acercar) 42 | plot(window(sbux.ts, start=c(2000,3), end=c(2008,3)), 43 | ylab="Adjusted close",col="blue", lwd=2, 44 | main="Monthly closing price of SBUX") 45 | 46 | 47 | # Plot para multiples columnas 48 | # En graficos diferentes 49 | plot(sbuxmsft.ts) 50 | 51 | 52 | # Plot en el mismo grafico 53 | plot(sbuxmsft.ts, plot.type="single", 54 | main="Monthly closing prices on SBUX and MSFT", 55 | ylab="Adjusted close price", 56 | col=c("blue", "red"), lty=1:2) 57 | legend(1994, 35, legend=c("SBUX","MSFT"), col=c("blue", "red"), 58 | lty=1:2) 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /11 Series temporales en R/msftPrices.csv: -------------------------------------------------------------------------------- 1 | "Date","Adj.Close" 2 | "3/31/1993",1.849489 3 | "4/1/1993",1.859486 4 | "5/3/1993",1.794505 5 | "6/1/1993",1.829494 6 | "7/1/1993",1.794505 7 | "8/2/1993",1.804501 8 | "9/1/1993",1.779508 9 | "10/1/1993",1.802002 10 | "11/1/1993",1.777009 11 | "12/1/1993",1.779508 12 | "1/3/1994",1.742019 13 | "2/1/1994",1.73952 14 | "3/1/1994",1.73952 15 | "4/4/1994",1.734521 16 | "5/2/1994",1.697031 17 | "6/1/1994",1.66454 18 | "7/1/1994",1.627051 19 | "8/1/1994",1.607056 20 | "9/1/1994",1.649544 21 | "10/3/1994",1.719525 22 | "11/1/1994",1.709528 23 | "12/1/1994",1.709528 24 | "1/3/1995",1.722024 25 | "2/1/1995",1.759514 26 | "3/1/1995",1.784507 27 | "4/3/1995",1.744518 28 | "5/1/1995",1.73952 29 | "6/1/1995",1.782007 30 | "7/3/1995",1.754516 31 | "8/1/1995",1.724523 32 | "9/1/1995",1.714527 33 | "10/2/1995",1.709528 34 | "11/1/1995",1.729522 35 | "12/1/1995",1.769511 36 | "1/2/1996",1.849489 37 | "2/1/1996",1.88198 38 | "3/1/1996",1.849489 39 | "4/1/1996",1.841991 40 | "5/1/1996",1.824496 41 | "6/3/1996",1.904474 42 | "7/1/1996",1.88198 43 | "8/1/1996",1.851989 44 | "9/3/1996",1.924468 45 | "10/1/1996",1.914471 46 | "11/1/1996",1.891977 47 | "12/2/1996",1.879481 48 | "1/2/1997",1.864485 49 | "2/3/1997",1.844489 50 | "3/3/1997",1.819497 51 | "4/1/1997",1.821996 52 | "5/1/1997",1.787006 53 | "6/2/1997",1.844489 54 | "7/1/1997",1.849489 55 | "8/1/1997",1.824496 56 | "9/2/1997",1.816998 57 | "10/1/1997",1.749517 58 | "11/3/1997",1.77201 59 | "12/1/1997",1.759514 60 | "1/2/1998",1.77201 61 | "2/2/1998",1.769511 62 | "3/2/1998",1.769511 63 | "4/1/1998",1.804501 64 | "5/1/1998",1.779508 65 | "6/1/1998",1.759514 66 | "7/1/1998",1.742019 67 | "8/3/1998",1.744518 68 | "9/1/1998",1.719525 69 | "10/1/1998",1.667039 70 | "11/2/1998",1.697031 71 | "12/1/1998",1.692032 72 | "1/4/1999",1.674537 73 | "2/1/1999",1.654542 74 | "3/1/1999",1.684535 75 | "4/1/1999",1.669538 76 | "5/3/1999",1.622051 77 | "6/1/1999",1.574565 78 | "7/1/1999",1.609555 79 | "8/2/1999",1.579563 80 | "9/1/1999",1.55207 81 | "10/1/1999",1.55207 82 | "11/1/1999",1.564567 83 | "12/1/1999",1.509583 84 | "1/3/2000",1.564567 85 | "2/1/2000",1.579563 86 | "3/1/2000",1.479591 87 | "4/3/2000",1.454598 88 | "5/1/2000",1.459597 89 | "6/1/2000",1.459597 90 | "7/3/2000",1.494587 91 | "8/1/2000",1.464595 92 | "9/1/2000",1.459597 93 | "10/2/2000",1.419607 94 | "11/1/2000",1.447101 95 | "12/1/2000",1.479591 96 | "1/2/2001",1.504584 97 | "2/1/2001",1.554571 98 | "3/1/2001",1.544573 99 | "4/2/2001",1.544573 100 | "5/1/2001",1.48459 101 | "6/1/2001",1.524579 102 | "7/2/2001",1.539575 103 | "8/1/2001",1.51708 104 | "9/4/2001",1.48459 105 | "10/1/2001",1.442101 106 | "11/1/2001",1.437103 107 | "12/3/2001",1.452098 108 | "1/2/2002",1.502084 109 | "2/4/2002",1.534575 110 | "3/1/2002",1.532077 111 | "4/1/2002",1.514581 112 | "5/1/2002",1.494587 113 | "6/3/2002",1.489588 114 | "7/1/2002",1.524579 115 | "8/1/2002",1.55957 116 | "9/3/2002",1.527078 117 | "10/1/2002",1.504584 118 | "11/1/2002",1.529578 119 | "12/2/2002",1.51958 120 | "1/2/2003",1.51958 121 | "2/3/2003",1.512082 122 | "3/3/2003",1.51958 123 | "4/1/2003",1.544573 124 | "5/1/2003",1.574565 125 | "6/2/2003",1.599558 126 | "7/1/2003",1.644545 127 | "8/1/2003",1.679536 128 | "9/2/2003",1.659541 129 | "10/1/2003",1.649544 130 | "11/3/2003",1.639547 131 | "12/1/2003",1.652043 132 | "1/2/2004",1.66454 133 | "2/2/2004",1.697031 134 | "3/1/2004",1.682035 135 | "4/1/2004",1.689533 136 | "5/3/2004",1.679536 137 | "6/1/2004",1.684535 138 | "7/1/2004",1.66454 139 | "8/2/2004",1.642046 140 | "9/1/2004",1.614554 141 | "10/1/2004",1.659541 142 | "11/1/2004",1.604556 143 | "12/1/2004",1.619552 144 | "1/3/2005",1.589561 145 | "2/1/2005",1.607056 146 | "3/1/2005",1.609555 147 | "4/1/2005",1.589561 148 | "5/2/2005",1.597059 149 | "6/1/2005",1.579563 150 | "7/1/2005",1.602057 151 | "8/1/2005",1.617053 152 | "9/1/2005",1.604556 153 | "10/3/2005",1.569566 154 | "11/1/2005",1.529578 155 | "12/1/2005",1.574565 156 | "1/3/2006",1.569566 157 | "2/1/2006",1.564567 158 | "3/1/2006",1.627051 159 | "4/3/2006",1.632049 160 | "5/1/2006",1.637048 161 | "6/1/2006",1.609555 162 | "7/3/2006",1.644545 163 | "8/1/2006",1.614554 164 | "9/1/2006",1.604556 165 | "10/2/2006",1.602057 166 | "11/1/2006",1.539575 167 | "12/1/2006",1.547072 168 | "1/3/2007",1.574565 169 | "2/1/2007",1.579563 170 | "3/1/2007",1.589561 171 | "4/2/2007",1.599558 172 | "5/1/2007",1.629549 173 | "6/1/2007",1.657043 174 | "7/2/2007",1.714527 175 | "8/1/2007",1.697031 176 | "9/4/2007",1.709528 177 | "10/1/2007",1.669538 178 | "11/1/2007",1.622051 179 | "12/3/2007",1.637048 180 | "1/2/2008",1.627051 181 | "2/1/2008",1.599558 182 | "3/3/2008",1.594559 183 | -------------------------------------------------------------------------------- /11 Series temporales en R/sbuxPrices.csv: -------------------------------------------------------------------------------- 1 | Date,Adj.Close 2 | 3/31/1993,1.13 3 | 4/1/1993,1.15 4 | 5/3/1993,1.43 5 | 6/1/1993,1.46 6 | 7/1/1993,1.41 7 | 8/2/1993,1.44 8 | 9/1/1993,1.63 9 | 10/1/1993,1.59 10 | 11/1/1993,1.32 11 | 12/1/1993,1.32 12 | 1/3/1994,1.43 13 | 2/1/1994,1.38 14 | 3/1/1994,1.45 15 | 4/4/1994,1.77 16 | 5/2/1994,1.69 17 | 6/1/1994,1.5 18 | 7/1/1994,1.72 19 | 8/1/1994,1.68 20 | 9/1/1994,1.37 21 | 10/3/1994,1.61 22 | 11/1/1994,1.59 23 | 12/1/1994,1.63 24 | 1/3/1995,1.43 25 | 2/1/1995,1.42 26 | 3/1/1995,1.43 27 | 4/3/1995,1.4 28 | 5/1/1995,1.73 29 | 6/1/1995,2.12 30 | 7/3/1995,2.22 31 | 8/1/1995,2.38 32 | 9/1/1995,2.25 33 | 10/2/1995,2.33 34 | 11/1/1995,2.51 35 | 12/1/1995,2.5 36 | 1/2/1996,1.99 37 | 2/1/1996,2.09 38 | 3/1/1996,2.77 39 | 4/1/1996,3.22 40 | 5/1/1996,3.22 41 | 6/3/1996,3.36 42 | 7/1/1996,3.09 43 | 8/1/1996,3.89 44 | 9/3/1996,3.92 45 | 10/1/1996,3.86 46 | 11/1/1996,4.12 47 | 12/2/1996,3.4 48 | 1/2/1997,4.07 49 | 2/3/1997,4 50 | 3/3/1997,3.52 51 | 4/1/1997,3.55 52 | 5/1/1997,3.74 53 | 6/2/1997,4.63 54 | 7/1/1997,4.87 55 | 8/1/1997,4.87 56 | 9/2/1997,4.97 57 | 10/1/1997,3.92 58 | 11/3/1997,4.15 59 | 12/1/1997,4.56 60 | 1/2/1998,4.35 61 | 2/2/1998,4.7 62 | 3/2/1998,5.39 63 | 4/1/1998,5.72 64 | 5/1/1998,5.71 65 | 6/1/1998,6.35 66 | 7/1/1998,4.98 67 | 8/3/1998,3.75 68 | 9/1/1998,4.3 69 | 10/1/1998,5.16 70 | 11/2/1998,5.48 71 | 12/1/1998,6.67 72 | 1/4/1999,6.19 73 | 2/1/1999,6.29 74 | 3/1/1999,6.67 75 | 4/1/1999,8.78 76 | 5/3/1999,8.77 77 | 6/1/1999,8.93 78 | 7/1/1999,5.53 79 | 8/2/1999,5.44 80 | 9/1/1999,5.89 81 | 10/1/1999,6.46 82 | 11/1/1999,6.31 83 | 12/1/1999,5.76 84 | 1/3/2000,7.61 85 | 2/1/2000,8.35 86 | 3/1/2000,10.65 87 | 4/3/2000,7.19 88 | 5/1/2000,8.08 89 | 6/1/2000,9.08 90 | 7/3/2000,8.91 91 | 8/1/2000,8.71 92 | 9/1/2000,9.52 93 | 10/2/2000,10.62 94 | 11/1/2000,10.83 95 | 12/1/2000,10.52 96 | 1/2/2001,11.87 97 | 2/1/2001,11.32 98 | 3/1/2001,10.09 99 | 4/2/2001,9.2 100 | 5/1/2001,9.28 101 | 6/1/2001,10.94 102 | 7/2/2001,8.58 103 | 8/1/2001,8.02 104 | 9/4/2001,7.1 105 | 10/1/2001,8.14 106 | 11/1/2001,8.42 107 | 12/3/2001,9.06 108 | 1/2/2002,11.3 109 | 2/4/2002,10.94 110 | 3/1/2002,11 111 | 4/1/2002,10.85 112 | 5/1/2002,11.54 113 | 6/3/2002,11.81 114 | 7/1/2002,9.33 115 | 8/1/2002,9.56 116 | 9/3/2002,9.82 117 | 10/1/2002,11.33 118 | 11/1/2002,10.34 119 | 12/2/2002,9.69 120 | 1/2/2003,10.8 121 | 2/3/2003,11.15 122 | 3/3/2003,12.25 123 | 4/1/2003,11.18 124 | 5/1/2003,11.73 125 | 6/2/2003,11.67 126 | 7/1/2003,12.99 127 | 8/1/2003,13.5 128 | 9/2/2003,13.69 129 | 10/1/2003,15.02 130 | 11/3/2003,15.3 131 | 12/1/2003,15.77 132 | 1/2/2004,17.41 133 | 2/2/2004,17.78 134 | 3/1/2004,18.01 135 | 4/1/2004,18.5 136 | 5/3/2004,19.3 137 | 6/1/2004,20.68 138 | 7/1/2004,22.34 139 | 8/2/2004,20.56 140 | 9/1/2004,21.61 141 | 10/1/2004,25.14 142 | 11/1/2004,26.75 143 | 12/1/2004,29.65 144 | 1/3/2005,25.67 145 | 2/1/2005,24.63 146 | 3/1/2005,24.56 147 | 4/1/2005,23.54 148 | 5/2/2005,26.05 149 | 6/1/2005,24.56 150 | 7/1/2005,24.98 151 | 8/1/2005,23.31 152 | 9/1/2005,23.82 153 | 10/3/2005,26.89 154 | 11/1/2005,28.95 155 | 12/1/2005,28.54 156 | 1/3/2006,30.14 157 | 2/1/2006,34.54 158 | 3/1/2006,35.78 159 | 4/3/2006,35.44 160 | 5/1/2006,33.9 161 | 6/1/2006,35.91 162 | 7/3/2006,32.55 163 | 8/1/2006,29.49 164 | 9/1/2006,32.38 165 | 10/2/2006,35.9 166 | 11/1/2006,33.56 167 | 12/1/2006,33.68 168 | 1/3/2007,33.22 169 | 2/1/2007,29.38 170 | 3/1/2007,29.82 171 | 4/2/2007,29.5 172 | 5/1/2007,27.4 173 | 6/1/2007,24.95 174 | 7/2/2007,25.37 175 | 8/1/2007,26.2 176 | 9/4/2007,24.91 177 | 10/1/2007,25.37 178 | 11/1/2007,22.24 179 | 12/3/2007,19.46 180 | 1/2/2008,17.98 181 | 2/1/2008,17.1 182 | 3/3/2008,16.64 183 | -------------------------------------------------------------------------------- /12 Mapas en R/Mapas en R.R: -------------------------------------------------------------------------------- 1 | # Mapas en R 2 | 3 | library(ggplot2) 4 | 5 | ################################# Italia 6 | italy_map <- map_data(map="italy") 7 | str(italy_map) 8 | head(italy_map) 9 | 10 | ## Construir un dataset con los valores para cada región del mapa (puede ser que ya se tenga) 11 | library(tibble) 12 | # Generamos algunos datos aleatorios 13 | # Si usamos nuestro propio dataset, solo nos aseguramos de contar con la columna 'region' y 'value' 14 | # Lo que necesitamos es un dataset que para cada región nos de un valor numérico 15 | reg_data <- tibble(region=unique(italy_map$region), 16 | value=sample(100, length(region))) 17 | 18 | ## Inicializamos un nuevo gráfico 19 | gg <- ggplot() 20 | # capa base 21 | # 'data=italy_map' y 'map=italy_map' nos sirven para indicarle las coordenadas que queremos graficar 22 | # 'map_id' nos permite identificar el mapa para agregar más información después 23 | gg <- gg + geom_map(data=italy_map, map=italy_map, 24 | aes(long, lat, map_id=region), 25 | color="#b2b2b2", size=0.1, fill=NA) 26 | 27 | ## Graficar el mapa: no tiene colores que diferencien las áreas (fill=NA): 28 | gg 29 | 30 | ## Vamos a rellenar las regiones con un color 31 | # queremos que el color se corresponda al valor de "value" 32 | # 'data=reg_data' es la información que queremos agregar (datos por regiones) 33 | # 'fill=value' nos indica qué columna de 'reg_data' usaremos para determinar el color 34 | gg <- gg + geom_map(data=reg_data, map=italy_map, 35 | aes(fill=value, map_id=region), 36 | color="#b2b2b2", size=0.1) 37 | gg 38 | 39 | # nuestro mapa está casi listo, pero se verá algo estirado 40 | # con 'coord_fixed' le daremos una apariencia cuadrada 41 | gg <- gg + coord_fixed(ratio = 1) 42 | gg 43 | 44 | 45 | 46 | ################################# USA states 47 | usa_map <- map_data("state") 48 | str(usa_map) 49 | head(usa_map) 50 | 51 | reg_data <- tibble(region=unique(usa_map$region), 52 | value=sample(100, length(region))) 53 | 54 | gg <- ggplot() 55 | 56 | gg <- gg + geom_map(data=usa_map, map=usa_map, 57 | aes(long, lat, map_id=region), 58 | color="#b2b2b2", size=0.1, fill=NA) 59 | gg 60 | 61 | 62 | gg <- gg + geom_map(data=reg_data, map=usa_map, 63 | aes(fill=value, map_id=region), 64 | color="#b2b2b2", size=0.1) 65 | gg 66 | 67 | gg <- gg + coord_fixed(ratio = 1.5) 68 | gg 69 | 70 | 71 | 72 | ################################# WORLD and WORLD2 TAREA 73 | world_map <- map_data("world") #world2 74 | str(world_map) 75 | head(world_map) 76 | 77 | reg_data <- tibble(region=unique(world_map$region), 78 | value=sample(300, length(region))) 79 | 80 | gg <- ggplot() 81 | 82 | gg <- gg + geom_map(data=world_map, map=world_map, 83 | aes(long, lat, map_id=region), 84 | color="#b2b2b2", size=0.1, fill=NA) 85 | gg 86 | 87 | 88 | gg <- gg + geom_map(data=reg_data, map=world_map, 89 | aes(fill=value, map_id=region), 90 | color="#b2b2b2", size=0.1) 91 | gg 92 | 93 | gg <- gg + coord_fixed(ratio = 1) 94 | gg 95 | 96 | -------------------------------------------------------------------------------- /12 Mapas en R/italy map R.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecabestadistica/curso-R-Python-data-science-analisis-datos/e5a67aab9fb39915dd039fd7debbba36200442b7/12 Mapas en R/italy map R.png -------------------------------------------------------------------------------- /3 Introduccion a Pandas/companies79.csv: -------------------------------------------------------------------------------- 1 | "","V1","V2","V3","V4","V5","V6","V7","V8" 2 | "1","BellAtlantic",19788,9084,10636,1092.9,2576.8,79.4,"Communication" 3 | "2","ContinentalTelecom",5074,2557,1892,239.9,578.3,21.9,"Communication" 4 | "3","AmericanElectricPower",13621,4848,4572,485,898.9,23.4,"Energy" 5 | "4","BrooklynUnionGas",1117,1038,478,59.7,91.7,3.8,"Energy" 6 | "5","CentralIllinoisPublicService",1633,701,679,74.3,135.9,2.8,"Energy" 7 | "6","ClevelandElectricIlluminating",5651,1254,2002,310.7,407.9,6.2,"Energy" 8 | "7","ColumbiaGasSystem",5835,4053,1601,93.8,173.8,10.8,"Energy" 9 | "8","FloridaProgress",3494,1653,1442,160.9,320.3,6.4,"Energy" 10 | "9","IdahoPower",1654,451,779,84.8,130.4,1.6,"Energy" 11 | "10","KansasPower&Light",1679,1354,687,93.8,154.6,4.6,"Energy" 12 | "11","MesaPetroleum",1257,355,181,167.5,304,0.6,"Energy" 13 | "12","MontanaPower",1743,597,717,121.6,172.4,3.5,"Energy" 14 | "13","PeoplesEnergy",1440,1617,639,81.7,126.4,3.5,"Energy" 15 | "14","PhillipsPetroleum",14045,15636,2754,418,1462,27.3,"Energy" 16 | "15","PublicServiceCoofNewMexico",3010,749,1120,146.3,209.2,3.4,"Energy" 17 | "16","SanDiegoGas&Electric",3086,1739,1507,202.7,335.2,4.9,"Energy" 18 | "17","ValeroEnergy",1995,2662,341,34.7,100.7,2.3,"Energy" 19 | "18","AmericanSavingsBankFSB",3614,367,90,14.1,24.6,1.1,"Finance" 20 | "19","BankSouth",2788,271,304,23.5,28.9,2.1,"Finance" 21 | "20","H&RBlock",327,542,959,54.1,72.5,2.8,"Finance" 22 | "21","CaliforniaFirstBank",5401,550,376,25.6,37.5,4.1,"Finance" 23 | "22","Cigna",44736,16197,4653,732.5,651.9,48.5,"Finance" 24 | "23","Dreyfus",401,176,1084,55.6,57,0.7,"Finance" 25 | "24","FirstAmerican",4789,453,367,40.2,51.4,3,"Finance" 26 | "25","FirstEmpireState",2548,264,181,22.2,26.2,2.1,"Finance" 27 | "26","FirstTennesseeNational",5249,527,346,37.8,56.2,4.1,"Finance" 28 | "27","MarineCorp",3720,356,211,26.6,34.8,2.4,"Finance" 29 | "28","MellonBank",33406,3222,1413,201.7,246.7,15.8,"Finance" 30 | "29","NationalCity",12505,1302,702,108.4,131.4,9,"Finance" 31 | "30","NorstarBancorp",8998,882,988,93,119,7.4,"Finance" 32 | "31","Norwest",21419,2516,930,107.6,164.7,15.6,"Finance" 33 | "32","SoutheastBanking",11052,1097,606,64.9,97.6,7,"Finance" 34 | "33","SovranFinancial",9672,1037,829,92.6,118.2,8.2,"Finance" 35 | "34","UnitedFinancialGroup",4989,518,53,3.1,0.3,0.8,"Finance" 36 | "35","AppleComputer",1022,1754,1370,72,119.5,4.8,"HiTech" 37 | "36","DigitalEquipment",6914,7029,7957,400.6,754.7,87.3,"HiTech" 38 | "37","Eg&G",430,1155,1045,55.7,70.8,22.5,"HiTech" 39 | "38","GeneralElectric",26432,28285,33172,2336,3562,304,"HiTech" 40 | "39","Hewlett-Packard",5769,6571,9462,482,792,83,"HiTech" 41 | "40","IBM",52634,50056,95697,6555,9874,400.2,"HiTech" 42 | "41","NCR",3940,4317,3940,315.2,566.3,62,"HiTech" 43 | "42","Telex",478,672,866,67.1,101.6,5.4,"HiTech" 44 | "43","ArmstrongWorldIndustries",1093,1679,1070,100.9,164.5,20.8,"Manufacturing" 45 | "44","CBIIndustries",1128,1516,430,47,26.7,13.2,"Manufacturing" 46 | "45","Fruehauf",1804,2564,483,70.5,164.9,26.6,"Manufacturing" 47 | "46","Halliburton",4662,4781,2988,28.7,371.5,66.2,"Manufacturing" 48 | "47","LTV",6307,8199,598,771.5,524.3,57.5,"Manufacturing" 49 | "48","Owens-CorningFiberglas",2366,3305,1117,131.2,256.5,25.2,"Manufacturing" 50 | "49","PPGIndustries",4084,4346,3023,302.7,521.7,37.5,"Manufacturing" 51 | "50","Textron",10348,5721,1915,223.6,322.5,49.5,"Manufacturing" 52 | "51","Turner",752,2149,101,11.1,15.2,2.6,"Manufacturing" 53 | "52","UnitedTechnologies",10528,14992,5377,312.7,710.7,184.8,"Manufacturing" 54 | "53","CommunityPsychiatricCenters",278,205,853,44.8,50.5,3.8,"Medical" 55 | "54","HospitalCorpofAmerica",6259,4152,3090,283.7,524.5,62,"Medical" 56 | "55","AHRobins",707,706,275,61.4,77.8,6.1,"Medical" 57 | "56","SharedMedicalSystems",252,312,883,41.7,60.6,3.3,"Medical" 58 | "57","AirProducts",2687,1870,1890,145.7,352.2,18.2,"Other" 59 | "58","AlliedSignal",13271,9115,8190,279,83,143.8,"Other" 60 | "59","BallyManufactoring",1529,1295,444,25.6,137,19.4,"Other" 61 | "60","CrownCork&Seal",866,1487,944,71.7,115.4,12.6,"Other" 62 | "61","Ex-Cell-0",799,1140,683,57.6,89.2,15.4,"Other" 63 | "62","LizClaiborne",223,557,1040,60.6,63.7,1.9,"Other" 64 | "63","WarnerCommunications",2286,2235,2306,195.3,219,8,"Other" 65 | "64","Dayton-Hudson",4418,8793,4459,283.6,456.5,128,"Retail" 66 | "65","DillardDepartmentStores",862,1601,1093,66.9,106.8,16,"Retail" 67 | "66","GiantFood",623,2247,797,57,93.8,18.6,"Retail" 68 | "67","GreatA&PTea",1608,6615,829,56.1,134,65,"Retail" 69 | "68","Kroger",4178,17124,2091,180.8,390.4,164.6,"Retail" 70 | "69","MayDepartmentStores",3442,5080,2673,235.4,361.5,77.3,"Retail" 71 | "70","Stop&ShopCos",1112,3689,542,30.3,96.9,43.5,"Retail" 72 | "71","SupermarketsGeneral",1104,5123,910,63.7,133.3,48.5,"Retail" 73 | "72","WickesCos",2957,2806,457,40.6,93.5,50,"Retail" 74 | "73","FWWoolworth",2535,5958,1921,177,288,118.1,"Retail" 75 | "74","AMR",6425,6131,2448,345.8,682.5,49.5,"Transportation" 76 | "75","IUInternational",999,1878,393,173.5,108.1,23.3,"Transportation" 77 | "76","PanAm",2448,3484,1036,48.8,257.1,25.4,"Transportation" 78 | "77","RepublicAirlines",1286,1734,361,69.2,145.7,14.3,"Transportation" 79 | "78","TWA",2769,3725,663,208.4,12.4,29.1,"Transportation" 80 | "79","WesternAirLines",952,1307,309,35.4,92.8,10.3,"Transportation" 81 | -------------------------------------------------------------------------------- /6 Mapas en Python/Datos.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ecabestadistica/curso-R-Python-data-science-analisis-datos/e5a67aab9fb39915dd039fd7debbba36200442b7/6 Mapas en Python/Datos.zip -------------------------------------------------------------------------------- /7 Introduccion a R/Introduccion a R.R: -------------------------------------------------------------------------------- 1 | ### Introducción a R 2 | 3 | ## Variables 4 | 5 | #integer (la "L" es para exigir que sea entera) 6 | x <- 3L 7 | typeof(x) 8 | 9 | #double 10 | y <- 3.3 11 | typeof(y) 12 | 13 | z <- 3 14 | typeof(z) 15 | 16 | #complex 17 | c <- 9 + 4i 18 | typeof(c) 19 | 20 | #string / character (vale comilla doble o simple) 21 | s <- "a" 22 | typeof(s) 23 | 24 | ch <- 'b' 25 | typeof(ch) 26 | 27 | #logical 28 | t <- T 29 | typeof(t) 30 | 31 | t <- TRUE 32 | typeof(t) 33 | 34 | f <- F 35 | typeof(f) 36 | 37 | f <- FALSE 38 | typeof(f) 39 | 40 | #vector de doubles 41 | v <- c(1,2,3) 42 | typeof(v) 43 | 44 | #vector de caracteres 45 | v <- c("a","b","c") 46 | typeof(v) 47 | 48 | ## Operaciones 49 | 50 | a <- 3 51 | b <- 5 52 | 53 | #suma 54 | a+b 55 | 56 | #resta 57 | a-b 58 | 59 | #división 60 | a/b 61 | 62 | #producto 63 | a*b 64 | 65 | #exponente 66 | b^a 67 | 68 | #división entera 69 | a%%b 70 | 71 | #raíz cuadrada (es una función) 72 | sqrt(a) 73 | sqrt(b^a) 74 | 75 | #operaciones con strings 76 | string1 <- "Me gusta" 77 | string2 <- "R" 78 | mensaje <- paste(string1,string2) 79 | mensaje 80 | 81 | #operaciones matemáticas y estadísticas con vectores 82 | x <- c(1,2,3,4,5,6,7,8,9,10,0) 83 | mean(x) #media 84 | var(x) #varianza 85 | sd(x) #standard deviation 86 | 87 | sum(x) #suma de todos los elementos 88 | prod(x) #producto de todos los elementos 89 | 90 | sin(x) 91 | cos(x) 92 | tan(x) 93 | sqrt(x) 94 | exp(x) 95 | log(x) 96 | 97 | max(x) 98 | min(x) 99 | 100 | sort(x) 101 | 102 | ## Generación de sucesiones 103 | 1:30 104 | 10*1:10 105 | 30:1 106 | 107 | #con seq(min,max,step) 108 | seq(1,30) #equivalente a 1:30 109 | seq(1,30,5) 110 | 111 | #con rep(valor o vector, numero de repeticiones) 112 | rep(1,10) 113 | rep(x,10) 114 | 115 | ## Operadores lógicos 116 | x==0 117 | x!=0 118 | x==10 119 | x<5 120 | x>=5 121 | 122 | ## Conversiones 123 | typeof(x) 124 | 125 | y=as.integer(x) 126 | y 127 | typeof(y) 128 | 129 | y=as.character(x) 130 | y 131 | typeof(y) 132 | 133 | y=as.factor(x) 134 | y 135 | typeof(y) 136 | 137 | y=as.logical(x) 138 | y 139 | typeof(y) 140 | 141 | y=as.null(x) 142 | y 143 | typeof(y) 144 | 145 | #Verificar un tipo de dato concreto 146 | is.integer(x) 147 | is.logical(x) 148 | is.character(x) 149 | is.numeric(x) 150 | 151 | ## Cadenas de texto 152 | install.packages("stringr") 153 | library(stringr) 154 | x <- c("manzana", "plátano", "pera") 155 | x 156 | #detectar qué elemento contiene la "e": 157 | str_detect(x, "e") 158 | #contar cuántos "e" hay contenidos en cada elemento: 159 | str_count(x, "e") 160 | #contar cuántas "a" hay contenidos en cada elemento: 161 | str_count(x, "a") 162 | #reemplazar las coincidencias 163 | str_replace(x, "[aeiou]", "-") #reemplazar la primera vocal (sin tilde) por un guión "-" 164 | str_replace_all(x, "[aeiou]", "-") #reemplazar todas las vocales (sin tilde) por un guión "-" 165 | str_replace_all(x, "[aeiouáéíóú]", "-") #reemplazar todas las vocales (con o sin tilde) por un guión "-" 166 | 167 | ## Matrices 168 | #crear una matriz de tres filas y cuatro columnas, con los numeros del 1:12: 169 | matrix(1:12, nrow = 3, ncol = 4) 170 | 171 | #crear una matriz de tres filas y cuatro columnas, con todos los elementos = 3: 172 | matrix(3, nrow = 3, ncol = 4) 173 | 174 | #unir vectores como filas o columnas 175 | v1 <- 1:5 176 | v2 <- 6:10 177 | v3 <- 11:15 178 | 179 | v1; v2; v3 180 | 181 | mi_matriz <- rbind(v1,v2,v3) #unir por filas (rows) 182 | mi_matriz 183 | mi_matriz <-cbind(v1,v2,v3) #unir por columnas (columns) 184 | mi_matriz 185 | 186 | # Operaciones con matrices 187 | mi_matriz + 100 188 | mi_matriz - 99 189 | mi_matriz * 50 190 | mi_matriz / 2 191 | mi_matriz ^ 3 192 | t(mi_matriz) #transpuesta 193 | 194 | ## Acceso a elementos 195 | # Elementos de un vector 196 | x <- seq(1,100,10) 197 | x 198 | x[1] #primer elemento del vector x 199 | x[2] #segundo elemento del vector x 200 | x[-1] #todos los elementos del vector x, excepto el primero 201 | x[-3] #todos los elementos del vector x, excepto el tercero 202 | x[1:3] #los tres primeros elementos de x 203 | x[c(1,3,10)] #primer, tercero y último elemento de x 204 | 205 | # Elementos de una matriz (uno) 206 | mi_matriz <- mi_matriz*10 207 | mi_matriz 208 | mi_matriz[1] #primer elemento de mi_matriz 209 | mi_matriz[2] #segundo elemento de mi_matriz 210 | mi_matriz[6] #sexto elemento de mi_matriz (cuenta fila por fila para cada columna) 211 | mi_matriz[-1] #todos los elementos excepto el primero. Devuelve un vector porque ya no cuadran las dimensiones 212 | mi_matriz[-3] #todos los elementos excepto el tercero 213 | mi_matriz[1:3] #los tres primeros elementos de mi_matriz. Devuelve un vector 214 | mi_matriz[c(1,3,10)] #primer, tercero y último elemento de mi_matriz. Devuelve un vector 215 | 216 | 217 | # Elementos de una matriz (especificando dos: fila x columna) 218 | mi_matriz 219 | mi_matriz[1,1] #fila 1 y columna 1 220 | mi_matriz[1,] #fila 1 y todas las columnas 221 | mi_matriz[,1] #columna 1 y todas las filas 222 | mi_matriz[1:3,] #tres primeras filas y todas las columnas 223 | mi_matriz[c(1,4),] #fila 1 y 4, y todas las columnas 224 | mi_matriz[,"v1"] #todas las filas de la columna llamada "v1" 225 | mi_matriz[,c("v1","v3")] #todas las filas de la columna llamada "v1" y la "v3" 226 | 227 | #usando condicionales 228 | mi_matriz>50 229 | mi_matriz[mi_matriz>50] 230 | 231 | #Nombrando dimensiones de matrices 232 | rownames(mi_matriz) 233 | rownames(mi_matriz) <- c("user1", "user2", "user3", "user4", "user5") 234 | rownames(mi_matriz) 235 | mi_matriz 236 | 237 | colnames(mi_matriz) 238 | colnames(mi_matriz) <- c("var1","var2","var3") 239 | mi_matriz 240 | 241 | mi_matriz["user1",] 242 | mi_matriz["user1","var2"] 243 | mi_matriz["user1",1:2] 244 | 245 | ## Dataframes 246 | # Convertir una matriz a dataframe 247 | df <- data.frame(mi_matriz) 248 | df 249 | head(df) 250 | 251 | #Acceso a elementos 252 | df[1,1] 253 | df[1:3,1:2] 254 | df[,2:3] 255 | 256 | df$var1 257 | df$var2 258 | df$var3 259 | 260 | #Subconjuntos 261 | #seleccionar todas las filas cuya variable 1 cumpla la condición, y devuelve todas las columnas 262 | df[df$var1>=30,] 263 | #con dos condiciones 264 | df[(df$var1>=30) & (df$var2>=100),] 265 | #devolver solamente dos columnas 266 | df[(df$var1>=30) & (df$var2>=100), c("var1","var2")] 267 | 268 | #Resumen estadístico 269 | summary(df) 270 | 271 | ## Crear una Función 272 | #que calcule la media de la columna elegida de un dataframe: 273 | funcion_media <- function(dataframe, columna) { 274 | mean(dataframe[,columna]) 275 | } 276 | 277 | #comprobar la funcion: 278 | funcion_media(df,1) 279 | #debe coincidir con: 280 | mean(df$var1) 281 | 282 | #comprobar la funcion: 283 | funcion_media(df,2) 284 | #debe coincidir con: 285 | mean(df$var2) 286 | 287 | #comprobar la funcion: 288 | funcion_media(df,3) 289 | #debe coincidir con: 290 | mean(df$var3) 291 | 292 | # Función que calcule la media de la columna elegida de un dataframe, y devuelva un mensaje si es mayor que 50: 293 | funcion_media <- function(dataframe, columna) { 294 | m=mean(dataframe[,columna]) 295 | if (m>50) { 296 | print("La media de esta columna es mayor que 50") 297 | } else { 298 | print("La media de esta columna NO es mayor que 50") 299 | } 300 | } 301 | 302 | #comprobar la funcion: 303 | funcion_media(df,1) 304 | mean(df$var1) 305 | 306 | #comprobar la funcion: 307 | funcion_media(df,2) 308 | mean(df$var2) 309 | 310 | #comprobar la funcion: 311 | funcion_media(df,3) 312 | mean(df$var3) 313 | 314 | # Función que calcule la media de todas las columnas del dataframe 315 | funcion_todas_medias <- function(dataframe) { 316 | #p: numero de columnas 317 | p=dim(dataframe)[2] 318 | 319 | #vector de ceros del tamaño de la dimensión 320 | mean_vector=rep(0,p) 321 | 322 | #para cada columna, calcula la media de la columna y guardala 323 | for (i in 1:p) { 324 | m=mean(dataframe[,i]) 325 | mean_vector[i]=m 326 | } 327 | #devuelve el vector de medias 328 | mean_vector 329 | } 330 | 331 | #comprobar la funcion: 332 | funcion_todas_medias(df) 333 | mean(df$var1) 334 | mean(df$var2) 335 | mean(df$var3) 336 | 337 | 338 | 339 | -------------------------------------------------------------------------------- /8 Importar datos en R/Importar datos en R.R: -------------------------------------------------------------------------------- 1 | ### Importar datos 2 | # Hay conjuntos de datos que ya están dentro de R, por ejemplo el USA states (datos de 50 estados de EE.UU) 3 | ?state.x77 4 | 5 | #Cargar los datos 6 | X <- as.data.frame(state.x77) 7 | X 8 | 9 | #Dimensiones 10 | dim(X) 11 | 12 | n.X <- nrow(X) 13 | n.X 14 | p.X <- ncol(X) 15 | p.X 16 | 17 | #Resumen estadístico de cada variable 18 | summary(X) 19 | 20 | #Vector de medias por columna 21 | mu.X <- colMeans(X) 22 | mu.X 23 | 24 | #Matriz de covarianza 25 | S.X <- cov(X) 26 | S.X 27 | 28 | #Matriz de correlaciones 29 | R.X <- cor(X) 30 | R.X 31 | 32 | #Seleccionando subconjuntos 33 | X[X$Population>9000,] 34 | 35 | X[(X$Population>9000) & (X$Income>5000),] 36 | 37 | subset=X[(X$Population>9000) & (X$Income>5000), c("Population", "Income", "Area")] 38 | subset 39 | 40 | 41 | ## Importar desde un .csv 42 | cancerdata <- read.csv("~/Documents/UDEMY Github/0. Curso R y Python/9 Importar y exportar datos/5 Importar y exportar datos en R/cancerdata.csv") 43 | 44 | #Dimensiones 45 | dim(cancerdata) 46 | 47 | 48 | #Resumen estadístico de cada variable 49 | summary(cancerdata) 50 | 51 | # Filtrar 52 | subset_cancerdata=cancerdata[(cancerdata$radius_mean>17) & (cancerdata$diagnosis=="M"),] 53 | subset_cancerdata 54 | dim(subset_cancerdata) 55 | 56 | ## Guardar un .csv 57 | write.csv(subset_cancerdata,"~/Documents/UDEMY Github/0. Curso R y Python/9 Importar y exportar datos/5 Importar y exportar datos en R/subset_cancerdata.csv") 58 | 59 | 60 | -------------------------------------------------------------------------------- /9 Visualizaciones basicas en R/Visualizaciones basicas en R.R: -------------------------------------------------------------------------------- 1 | ### Visualizaciones básicas en R 2 | 3 | # Cargar los datos 4 | X <- as.data.frame(state.x77) 5 | X 6 | 7 | #Boxplot 8 | boxplot(X$Population) 9 | boxplot(X$Population, col="blue") 10 | boxplot(X) 11 | ?boxplot 12 | 13 | #Histograma 14 | hist(X[,"Murder"],main="Murder Histogram",xlab="",col="cyan") 15 | 16 | #Scatterplot (pch: símbolo) 17 | plot(X$Income,X$Population,pch=5,col="magenta",xlab="Income",ylab="Population") 18 | 19 | pairs(X,pch=19,col="blue") 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Curso de R y Python para Data Science y Análisis de Datos 2 | 3 | ## Aprende R y Python desde cero para Análisis de Datos, Data Sicence, Minería de Datos, Data Mining, Visualizaciones, etc. 4 | 5 | Este curso es para aprender R y Python desde cero mientras programas. Es un curso completamente práctico en el que tendrás la base necesaria para dominar y realizar análisis más avanzados con ambos programas. 6 | 7 | Comenzaremos con la instalación de todo lo necesario para programar en nuestro ordenador y te enseñaremos también las herramientas de la nube que tienes disponible de forma gratuita para que ni siquiera tengas que instalar nada. 8 | 9 | Luego veremos los fundamentos principales de cada lenguaje, cómo se configura, cómo se trabaja, cómo se definen las cosas, cómo se crean funciones y códigos, etc. Posteriormente, veremos los diferentes paquetes y librerías más usadas (NumPy, Pandas, Matplotlib, Seaborn, ggplot2, etc.) de cada lenguaje, que podemos utilizar en nuestros análisis de datos. 10 | 11 | Y paso a paso veremos las diferentes formas de trabajar con los datos: cómo importarlos, cómo guardarlos, cómo descargar datos de internet y usarlos para analizarlos, cuáles son las principales partes del pre-procesado de datos, el análisis estadístico descriptivo, los diferentes gráficos que podemos crear en base a nuestros datos, y cómo podemos personalizar todas esas visualizaciones. 12 | 13 | Todo ello utlizando múltiples casos prácticos reales. Y a su vez, tendrás a tu disposición muchísimo material complementario gratuito así como todos los códigos y scripts tanto de R como de Python, para que todo ese conocimiento lo puedas transferir rápidamente a tu propio campo y tus propios análisis. 14 | 15 | Así que si tomas este curso estarás preparado para manejar R y Python con total soltura, y podrás comenzar a estudiar métodos un poco más avanzados del análisis de datos programados en R o Python. 16 | 17 | Más cursos y material gratuito en: www.aprendeconeli.com 18 | 19 | 20 | --------------------------------------------------------------------------------