{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" }, "toc": true }, "source": [ "

Table of Contents

\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Types of Operators" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "Arithmetic Operators `(+ - * / %%` (modulo) `%/%`(integer divide) ` ^`(raised to the power of)) \n", "Relational Operators`(> < <= >= == != )` \n", "Logical Operators `(& | ! && ||)` \n", "Assignment Operators `(<− = <<− -> ->>)` \n", "Miscellaneous Operators `(: %in% %*%)`" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Arithmetic Operators\n", "\n", "Operator Description Example\n", "- Subtraction 5 - 1 = 4\n", "+ Addition 5 + 1 = 6\n", "* Multiplication 5 * 3 = 15\n", "/ Division 10 / 2 = 5\n", "^ or ** Exponentiation 2*2*2*2*2 as 2 to the power of 5\n", "x%%y Modulus 5%%2 is 1\n", "x%/%y Integer Division 5%/%2 is 2" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "Relational and Logical Operators\n", "\n", "Operator Description Example\n", "< less than 5 < 10\n", "<= less than or equal to <= 5\n", "> greater than 10 > 5\n", ">= greater than or equal to >= 10\n", "== exactly equal to == 10\n", "!= not equal to != 5\n", "!x not x x <- c(5), !x\n", "x | y x or y x <- c(5), y <- c(10), x | y\n", "x & y x and y x <- c(5), y <- c(10), x & y\n", "isTRUE(x) tests whether x is true x <- TRUE, isTRUE(x)\n", " [1] FALSE" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Logical AND (`&&`) and Logical OR (`||`)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] TRUE\n" ] } ], "source": [ "# Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE.\n", "v <- c(3,0,TRUE,2+2i)\n", "t <- c(1,3,TRUE,2+3i)\n", "print(v&&t)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] FALSE\n" ] } ], "source": [ "# Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE.\n", "v <- c(0,0,TRUE,2+2i)\n", "t <- c(0,3,TRUE,2+3i)\n", "print(v||t)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## `%in%` Operator" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] TRUE\n", "[1] FALSE\n" ] } ], "source": [ "# This operator is used to identify if an element belongs to a vector.\n", "\n", "v1 <- 8\n", "v2 <- 12\n", "t <- 1:10\n", "print(v1 %in% t) \n", "print(v2 %in% t)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Control Structures or Conditional Statements\n", "\n", "(decision making statements)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "![Imgur](https://i.imgur.com/YQfqXlY.png?1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### If Condition\n", "**IF statement associates a condition with a sequence of statements, The sequence of statements is executed only if the condition is true. If the condition is false or null, the IF statement does nothing. In either case, control passes to the next statement**" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Num1 is less or equal to Num2\"\n" ] } ], "source": [ "num1=10\n", "num2=20\n", " \n", "if(num1<=num2){\n", "print(\"Num1 is less or equal to Num2\")\n", " }" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### If..Else" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"x is less than 10\"\n" ] } ], "source": [ "x <- 1:15\n", "if (sample(x, 1) <= 10) {\n", " print(\"x is less than 10\")\n", "} else {\n", " print(\"x is greater than 10\")\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Another way in R**" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'x less than 10'
  2. \n", "\t
  3. 'x less than 10'
  4. \n", "\t
  5. 'x less than 10'
  6. \n", "\t
  7. 'x less than 10'
  8. \n", "\t
  9. 'x less than 10'
  10. \n", "\t
  11. 'x less than 10'
  12. \n", "\t
  13. 'x less than 10'
  14. \n", "\t
  15. 'x less than 10'
  16. \n", "\t
  17. 'x less than 10'
  18. \n", "\t
  19. 'x less than 10'
  20. \n", "\t
  21. 'x greater than 10'
  22. \n", "\t
  23. 'x greater than 10'
  24. \n", "\t
  25. 'x greater than 10'
  26. \n", "\t
  27. 'x greater than 10'
  28. \n", "\t
  29. 'x greater than 10'
  30. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x less than 10'\n", "\\item 'x greater than 10'\n", "\\item 'x greater than 10'\n", "\\item 'x greater than 10'\n", "\\item 'x greater than 10'\n", "\\item 'x greater than 10'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'x less than 10'\n", "2. 'x less than 10'\n", "3. 'x less than 10'\n", "4. 'x less than 10'\n", "5. 'x less than 10'\n", "6. 'x less than 10'\n", "7. 'x less than 10'\n", "8. 'x less than 10'\n", "9. 'x less than 10'\n", "10. 'x less than 10'\n", "11. 'x greater than 10'\n", "12. 'x greater than 10'\n", "13. 'x greater than 10'\n", "14. 'x greater than 10'\n", "15. 'x greater than 10'\n", "\n", "\n" ], "text/plain": [ " [1] \"x less than 10\" \"x less than 10\" \"x less than 10\" \n", " [4] \"x less than 10\" \"x less than 10\" \"x less than 10\" \n", " [7] \"x less than 10\" \"x less than 10\" \"x less than 10\" \n", "[10] \"x less than 10\" \"x greater than 10\" \"x greater than 10\"\n", "[13] \"x greater than 10\" \"x greater than 10\" \"x greater than 10\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x <- 1:15\n", "ifelse(x <= 10, \"x less than 10\", \"x greater than 10\")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### If...Else...If" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"10 is equal to 10\"\n" ] } ], "source": [ "if (10==10)\n", " print('10 is equal to 10') else\n", " print('invalid')" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "0" ], "text/latex": [ "0" ], "text/markdown": [ "0" ], "text/plain": [ "[1] 0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "x <- 3\n", "y <- if(x>2){0}else{1}\n", "y" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 0
  4. \n", "\t
  5. 0
  6. \n", "\t
  7. 0
  8. \n", "\t
  9. 0
  10. \n", "\t
  11. 0
  12. \n", "\t
  13. 0
  14. \n", "\t
  15. 0
  16. \n", "\t
  17. 1
  18. \n", "\t
  19. 1
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 0\n", "3. 0\n", "4. 0\n", "5. 0\n", "6. 0\n", "7. 0\n", "8. 0\n", "9. 1\n", "10. 1\n", "\n", "\n" ], "text/plain": [ " [1] 1 0 0 0 0 0 0 0 1 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "z <- 1:10\n", "ifelse(z<2 | z>8, 1, 0)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'FALSE'
  2. \n", "\t
  3. 'FALSE'
  4. \n", "\t
  5. 'FALSE'
  6. \n", "\t
  7. 'FALSE'
  8. \n", "\t
  9. 'FALSE'
  10. \n", "\t
  11. 'TRUE'
  12. \n", "\t
  13. 'TRUE'
  14. \n", "\t
  15. 'TRUE'
  16. \n", "\t
  17. 'TRUE'
  18. \n", "\t
  19. 'TRUE'
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'FALSE'\n", "\\item 'FALSE'\n", "\\item 'FALSE'\n", "\\item 'FALSE'\n", "\\item 'FALSE'\n", "\\item 'TRUE'\n", "\\item 'TRUE'\n", "\\item 'TRUE'\n", "\\item 'TRUE'\n", "\\item 'TRUE'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'FALSE'\n", "2. 'FALSE'\n", "3. 'FALSE'\n", "4. 'FALSE'\n", "5. 'FALSE'\n", "6. 'TRUE'\n", "7. 'TRUE'\n", "8. 'TRUE'\n", "9. 'TRUE'\n", "10. 'TRUE'\n", "\n", "\n" ], "text/plain": [ " [1] \"FALSE\" \"FALSE\" \"FALSE\" \"FALSE\" \"FALSE\" \"TRUE\" \"TRUE\" \"TRUE\" \"TRUE\" \n", "[10] \"TRUE\" " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ifelse(z>5,'TRUE','FALSE')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. '?'
  2. \n", "\t
  3. '?'
  4. \n", "\t
  5. '?'
  6. \n", "\t
  7. '?'
  8. \n", "\t
  9. '?'
  10. \n", "\t
  11. '?'
  12. \n", "\t
  13. '?'
  14. \n", "\t
  15. '?'
  16. \n", "\t
  17. 'x'
  18. \n", "\t
  19. 'x'
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item '?'\n", "\\item '?'\n", "\\item '?'\n", "\\item '?'\n", "\\item '?'\n", "\\item '?'\n", "\\item '?'\n", "\\item '?'\n", "\\item 'x'\n", "\\item 'x'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. '?'\n", "2. '?'\n", "3. '?'\n", "4. '?'\n", "5. '?'\n", "6. '?'\n", "7. '?'\n", "8. '?'\n", "9. 'x'\n", "10. 'x'\n", "\n", "\n" ], "text/plain": [ " [1] \"?\" \"?\" \"?\" \"?\" \"?\" \"?\" \"?\" \"?\" \"x\" \"x\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "z <- 1:10\n", "ifelse(z>4&z>8,\"x\",\"?\")" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"truth is found the second time\"\n" ] } ], "source": [ "x <- c(\"what\",\"is\",\"truth\")\n", "\n", "if(\"Truth\" %in% x) {\n", " print(\"Truth is found the first time\")\n", "} else if (\"truth\" %in% x) {\n", " print(\"truth is found the second time\")\n", "} else {\n", " print(\"No truth found\")\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check this link to go through more examples of writing 'if-else' statements in R. \n", "https://www.programiz.com/r/if-else " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## For and While" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### For Loop\n", "To repeats a statement or group of for a fixed number of times." ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "run_control": { "frozen": false, "marked": false, "read_only": false }, "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"aaa\"\n", "[1] \"bbb\"\n", "[1] \"ccc\"\n" ] } ], "source": [ "vector <- c(\"aaa\",\"bbb\",\"ccc\")\n", " for(i in vector){ \n", " print(i) \n", "} " ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"The year is 2010\"\n", "[1] \"The year is 2011\"\n", "[1] \"The year is 2012\"\n", "[1] \"The year is 2013\"\n", "[1] \"The year is 2014\"\n", "[1] \"The year is 2015\"\n" ] } ], "source": [ "for (year in c(2010,2011,2012,2013,2014,2015)){\n", " print(paste(\"The year is\", year))\n", "}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "scrolled": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 3\n", "[1] 4\n", "[1] 5\n", "[1] 6\n" ] } ], "source": [ "for(i in 2:5){\n", " z <- i +1\n", " print(z)\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "| Instead of | Use |\n", "|-------------|--------------|\n", "| 1:length(x) | seq_along(x) |\n", "| 1:n | seq_len(n) |" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"a\"\n", "[1] \"b\"\n", "[1] \"c\"\n", "[1] \"d\"\n", "[1] \"a\"\n", "[1] \"b\"\n", "[1] \"c\"\n", "[1] \"d\"\n" ] } ], "source": [ "x <- c('a', 'b', 'c', 'd')\n", "for (i in seq_along(x)){\n", " print(x[i])\n", "}\n", "\n", "#alternatively\n", "for (words in x){\n", " print(words)\n", "}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "scrolled": false, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
147
258
369
\n" ], "text/latex": [ "\\begin{tabular}{lll}\n", "\t 1 & 4 & 7\\\\\n", "\t 2 & 5 & 8\\\\\n", "\t 3 & 6 & 9\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| 1 | 4 | 7 | \n", "| 2 | 5 | 8 | \n", "| 3 | 6 | 9 | \n", "\n", "\n" ], "text/plain": [ " [,1] [,2] [,3]\n", "[1,] 1 4 7 \n", "[2,] 2 5 8 \n", "[3,] 3 6 9 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 4\n", "[1] 7\n", "[1] 2\n", "[1] 5\n", "[1] 8\n", "[1] 3\n", "[1] 6\n", "[1] 9\n" ] } ], "source": [ "mymat <- matrix(1:9,3,3)\n", "mymat\n", "\n", "for (i in seq_len(nrow(mymat))){\n", " for (j in seq_len(ncol(mymat))){\n", " print(mymat[i,j])\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
heightweight
58 115
59 117
60 120
61 123
62 126
63 129
64 132
65 135
66 139
67 142
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " height & weight\\\\\n", "\\hline\n", "\t 58 & 115\\\\\n", "\t 59 & 117\\\\\n", "\t 60 & 120\\\\\n", "\t 61 & 123\\\\\n", "\t 62 & 126\\\\\n", "\t 63 & 129\\\\\n", "\t 64 & 132\\\\\n", "\t 65 & 135\\\\\n", "\t 66 & 139\\\\\n", "\t 67 & 142\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "height | weight | \n", "|---|---|---|---|---|---|---|---|---|---|\n", "| 58 | 115 | \n", "| 59 | 117 | \n", "| 60 | 120 | \n", "| 61 | 123 | \n", "| 62 | 126 | \n", "| 63 | 129 | \n", "| 64 | 132 | \n", "| 65 | 135 | \n", "| 66 | 139 | \n", "| 67 | 142 | \n", "\n", "\n" ], "text/plain": [ " height weight\n", "1 58 115 \n", "2 59 117 \n", "3 60 120 \n", "4 61 123 \n", "5 62 126 \n", "6 63 129 \n", "7 64 132 \n", "8 65 135 \n", "9 66 139 \n", "10 67 142 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[1] 86.5\n", "[1] 88\n", "[1] 90\n", "[1] 92\n", "[1] 94\n", "[1] 96\n", "[1] 98\n", "[1] 100\n", "[1] 102.5\n", "[1] 104.5\n" ] } ], "source": [ "x <- head(women,10)\n", "x\n", "mrnull <- NULL\n", "for(i in seq_along(x[,1])){\n", " concat <- c(mrnull, mean(as.numeric(x[i, 1:2])))\n", " print(concat)\n", "}\n" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ", , 1\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 1 2 3 4 5 6 7 8 9 10\n", " [2,] 2 4 6 8 10 12 14 16 18 20\n", " [3,] 3 6 9 12 15 18 21 24 27 30\n", " [4,] 4 8 12 16 20 24 28 32 36 40\n", " [5,] 5 10 15 20 25 30 35 40 45 50\n", " [6,] 6 12 18 24 30 36 42 48 54 60\n", " [7,] 7 14 21 28 35 42 49 56 63 70\n", " [8,] 8 16 24 32 40 48 56 64 72 80\n", " [9,] 9 18 27 36 45 54 63 72 81 90\n", "[10,] 10 20 30 40 50 60 70 80 90 100\n", "\n", ", , 2\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 2 4 6 8 10 12 14 16 18 20\n", " [2,] 4 8 12 16 20 24 28 32 36 40\n", " [3,] 6 12 18 24 30 36 42 48 54 60\n", " [4,] 8 16 24 32 40 48 56 64 72 80\n", " [5,] 10 20 30 40 50 60 70 80 90 100\n", " [6,] 12 24 36 48 60 72 84 96 108 120\n", " [7,] 14 28 42 56 70 84 98 112 126 140\n", " [8,] 16 32 48 64 80 96 112 128 144 160\n", " [9,] 18 36 54 72 90 108 126 144 162 180\n", "[10,] 20 40 60 80 100 120 140 160 180 200\n", "\n", ", , 3\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 3 6 9 12 15 18 21 24 27 30\n", " [2,] 6 12 18 24 30 36 42 48 54 60\n", " [3,] 9 18 27 36 45 54 63 72 81 90\n", " [4,] 12 24 36 48 60 72 84 96 108 120\n", " [5,] 15 30 45 60 75 90 105 120 135 150\n", " [6,] 18 36 54 72 90 108 126 144 162 180\n", " [7,] 21 42 63 84 105 126 147 168 189 210\n", " [8,] 24 48 72 96 120 144 168 192 216 240\n", " [9,] 27 54 81 108 135 162 189 216 243 270\n", "[10,] 30 60 90 120 150 180 210 240 270 300\n", "\n", ", , 4\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 4 8 12 16 20 24 28 32 36 40\n", " [2,] 8 16 24 32 40 48 56 64 72 80\n", " [3,] 12 24 36 48 60 72 84 96 108 120\n", " [4,] 16 32 48 64 80 96 112 128 144 160\n", " [5,] 20 40 60 80 100 120 140 160 180 200\n", " [6,] 24 48 72 96 120 144 168 192 216 240\n", " [7,] 28 56 84 112 140 168 196 224 252 280\n", " [8,] 32 64 96 128 160 192 224 256 288 320\n", " [9,] 36 72 108 144 180 216 252 288 324 360\n", "[10,] 40 80 120 160 200 240 280 320 360 400\n", "\n", ", , 5\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 5 10 15 20 25 30 35 40 45 50\n", " [2,] 10 20 30 40 50 60 70 80 90 100\n", " [3,] 15 30 45 60 75 90 105 120 135 150\n", " [4,] 20 40 60 80 100 120 140 160 180 200\n", " [5,] 25 50 75 100 125 150 175 200 225 250\n", " [6,] 30 60 90 120 150 180 210 240 270 300\n", " [7,] 35 70 105 140 175 210 245 280 315 350\n", " [8,] 40 80 120 160 200 240 280 320 360 400\n", " [9,] 45 90 135 180 225 270 315 360 405 450\n", "[10,] 50 100 150 200 250 300 350 400 450 500\n", "\n", ", , 6\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 6 12 18 24 30 36 42 48 54 60\n", " [2,] 12 24 36 48 60 72 84 96 108 120\n", " [3,] 18 36 54 72 90 108 126 144 162 180\n", " [4,] 24 48 72 96 120 144 168 192 216 240\n", " [5,] 30 60 90 120 150 180 210 240 270 300\n", " [6,] 36 72 108 144 180 216 252 288 324 360\n", " [7,] 42 84 126 168 210 252 294 336 378 420\n", " [8,] 48 96 144 192 240 288 336 384 432 480\n", " [9,] 54 108 162 216 270 324 378 432 486 540\n", "[10,] 60 120 180 240 300 360 420 480 540 600\n", "\n", ", , 7\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 7 14 21 28 35 42 49 56 63 70\n", " [2,] 14 28 42 56 70 84 98 112 126 140\n", " [3,] 21 42 63 84 105 126 147 168 189 210\n", " [4,] 28 56 84 112 140 168 196 224 252 280\n", " [5,] 35 70 105 140 175 210 245 280 315 350\n", " [6,] 42 84 126 168 210 252 294 336 378 420\n", " [7,] 49 98 147 196 245 294 343 392 441 490\n", " [8,] 56 112 168 224 280 336 392 448 504 560\n", " [9,] 63 126 189 252 315 378 441 504 567 630\n", "[10,] 70 140 210 280 350 420 490 560 630 700\n", "\n", ", , 8\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 8 16 24 32 40 48 56 64 72 80\n", " [2,] 16 32 48 64 80 96 112 128 144 160\n", " [3,] 24 48 72 96 120 144 168 192 216 240\n", " [4,] 32 64 96 128 160 192 224 256 288 320\n", " [5,] 40 80 120 160 200 240 280 320 360 400\n", " [6,] 48 96 144 192 240 288 336 384 432 480\n", " [7,] 56 112 168 224 280 336 392 448 504 560\n", " [8,] 64 128 192 256 320 384 448 512 576 640\n", " [9,] 72 144 216 288 360 432 504 576 648 720\n", "[10,] 80 160 240 320 400 480 560 640 720 800\n", "\n", ", , 9\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 9 18 27 36 45 54 63 72 81 90\n", " [2,] 18 36 54 72 90 108 126 144 162 180\n", " [3,] 27 54 81 108 135 162 189 216 243 270\n", " [4,] 36 72 108 144 180 216 252 288 324 360\n", " [5,] 45 90 135 180 225 270 315 360 405 450\n", " [6,] 54 108 162 216 270 324 378 432 486 540\n", " [7,] 63 126 189 252 315 378 441 504 567 630\n", " [8,] 72 144 216 288 360 432 504 576 648 720\n", " [9,] 81 162 243 324 405 486 567 648 729 810\n", "[10,] 90 180 270 360 450 540 630 720 810 900\n", "\n", ", , 10\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 10 20 30 40 50 60 70 80 90 100\n", " [2,] 20 40 60 80 100 120 140 160 180 200\n", " [3,] 30 60 90 120 150 180 210 240 270 300\n", " [4,] 40 80 120 160 200 240 280 320 360 400\n", " [5,] 50 100 150 200 250 300 350 400 450 500\n", " [6,] 60 120 180 240 300 360 420 480 540 600\n", " [7,] 70 140 210 280 350 420 490 560 630 700\n", " [8,] 80 160 240 320 400 480 560 640 720 800\n", " [9,] 90 180 270 360 450 540 630 720 810 900\n", "[10,] 100 200 300 400 500 600 700 800 900 1000\n", "\n", ", , 11\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 11 22 33 44 55 66 77 88 99 110\n", " [2,] 22 44 66 88 110 132 154 176 198 220\n", " [3,] 33 66 99 132 165 198 231 264 297 330\n", " [4,] 44 88 132 176 220 264 308 352 396 440\n", " [5,] 55 110 165 220 275 330 385 440 495 550\n", " [6,] 66 132 198 264 330 396 462 528 594 660\n", " [7,] 77 154 231 308 385 462 539 616 693 770\n", " [8,] 88 176 264 352 440 528 616 704 792 880\n", " [9,] 99 198 297 396 495 594 693 792 891 990\n", "[10,] 110 220 330 440 550 660 770 880 990 1100\n", "\n", ", , 12\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 12 24 36 48 60 72 84 96 108 120\n", " [2,] 24 48 72 96 120 144 168 192 216 240\n", " [3,] 36 72 108 144 180 216 252 288 324 360\n", " [4,] 48 96 144 192 240 288 336 384 432 480\n", " [5,] 60 120 180 240 300 360 420 480 540 600\n", " [6,] 72 144 216 288 360 432 504 576 648 720\n", " [7,] 84 168 252 336 420 504 588 672 756 840\n", " [8,] 96 192 288 384 480 576 672 768 864 960\n", " [9,] 108 216 324 432 540 648 756 864 972 1080\n", "[10,] 120 240 360 480 600 720 840 960 1080 1200\n", "\n", ", , 13\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 13 26 39 52 65 78 91 104 117 130\n", " [2,] 26 52 78 104 130 156 182 208 234 260\n", " [3,] 39 78 117 156 195 234 273 312 351 390\n", " [4,] 52 104 156 208 260 312 364 416 468 520\n", " [5,] 65 130 195 260 325 390 455 520 585 650\n", " [6,] 78 156 234 312 390 468 546 624 702 780\n", " [7,] 91 182 273 364 455 546 637 728 819 910\n", " [8,] 104 208 312 416 520 624 728 832 936 1040\n", " [9,] 117 234 351 468 585 702 819 936 1053 1170\n", "[10,] 130 260 390 520 650 780 910 1040 1170 1300\n", "\n", ", , 14\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 14 28 42 56 70 84 98 112 126 140\n", " [2,] 28 56 84 112 140 168 196 224 252 280\n", " [3,] 42 84 126 168 210 252 294 336 378 420\n", " [4,] 56 112 168 224 280 336 392 448 504 560\n", " [5,] 70 140 210 280 350 420 490 560 630 700\n", " [6,] 84 168 252 336 420 504 588 672 756 840\n", " [7,] 98 196 294 392 490 588 686 784 882 980\n", " [8,] 112 224 336 448 560 672 784 896 1008 1120\n", " [9,] 126 252 378 504 630 756 882 1008 1134 1260\n", "[10,] 140 280 420 560 700 840 980 1120 1260 1400\n", "\n", ", , 15\n", "\n", " [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n", " [1,] 15 30 45 60 75 90 105 120 135 150\n", " [2,] 30 60 90 120 150 180 210 240 270 300\n", " [3,] 45 90 135 180 225 270 315 360 405 450\n", " [4,] 60 120 180 240 300 360 420 480 540 600\n", " [5,] 75 150 225 300 375 450 525 600 675 750\n", " [6,] 90 180 270 360 450 540 630 720 810 900\n", " [7,] 105 210 315 420 525 630 735 840 945 1050\n", " [8,] 120 240 360 480 600 720 840 960 1080 1200\n", " [9,] 135 270 405 540 675 810 945 1080 1215 1350\n", "[10,] 150 300 450 600 750 900 1050 1200 1350 1500\n", "\n" ] } ], "source": [ "# Create your three-dimensional array\n", "my_array <- array(1:20, dim=c(20, 20, 20))\n", "\n", "for (i in 1:dim(my_array)[1]) {\n", " for (j in 1:dim(my_array)[2]) {\n", " for (k in 1:dim(my_array)[3]) {\n", " my_array[i,j,k] = i*j*k\n", " }\n", " }\n", "}\n", "\n", "# Show a 10x10x15 chunk of your array\n", "print(my_array[1:10, 1:10, 1:15])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### While Loop\n", "Loop until a specific condition is met" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 2\n", "[1] 3\n", "[1] 4\n", "[1] 5\n" ] } ], "source": [ "i <- 1\n", "\n", "while (i < 6) {\n", " print(i)\n", " i = i+1\n", "}" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "scrolled": true, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 0\n", "[1] 1\n", "[1] 1\n", "[1] 2\n", "[1] 3\n", "[1] 5\n", "[1] 8\n", "[1] 13\n", "[1] 21\n", "[1] 34\n" ] } ], "source": [ "a <- 0\n", "b <- 1\n", "print(a)\n", "while (b < 50) {\n", " print(b)\n", " temp <- a + b\n", " a <- b\n", " b <- temp\n", "}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 0\n", "[1] 1\n", "[1] 1\n", "[1] 2\n", "[1] 3\n", "[1] 5\n", "[1] 8\n", "[1] 13\n", "[1] 21\n", "[1] 34\n" ] } ], "source": [ "a <- 0\n", "b <- 1\n", "print(a)\n", "while (b < 50) {\n", " print(b)\n", " temp <- a + b\n", " a <- b\n", " b <- temp\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## repeat, break, next and switch" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Repeat statement\n", "Iterate over a block of code multiple number of times." ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 2\n", "[1] 3\n", "[1] 4\n", "[1] 5\n" ] } ], "source": [ "x <- 1\n", "\n", "repeat {\n", " print(x)\n", " x = x+1\n", " if (x == 6){\n", " break\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Break Statement\n", "break is used inside any loop like `repeat`, `for` or `while` to stop the iterations and flow the control outside of the loop." ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 2\n" ] } ], "source": [ "x <- 1:5\n", "\n", "for (val in x) {\n", " if (val == 3){\n", " break\n", " }\n", " print(val)\n", "}" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"1 and so on. \"\n", "[1] \"2 and so on. \"\n", "[1] \"3 and so on. \"\n", "[1] \"4 and so on. \"\n", "[1] \"5 and so on. \"\n" ] } ], "source": [ "x <- 1:10\n", "for (num in x){\n", " if (num==6) break\n", " mynum <- paste(num, \"and so on. \", sep = \" \")\n", " print(mynum)\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Next Statment\n", "* Useful to controls the flow of R loops \n", "* generaal usage inside the For Loop and While Loop " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Example:**" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 2\n", "[1] 4\n", "[1] 5\n" ] } ], "source": [ "x <- 1:5\n", "\n", "for (val in x) {\n", " if (val == 3){\n", " next\n", " }\n", " print(val)\n", "}" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 3
  4. \n", "\t
  5. 5
  6. \n", "\t
  7. 7
  8. \n", "\t
  9. 9
  10. \n", "\t
  11. 11
  12. \n", "\t
  13. 13
  14. \n", "\t
  15. 15
  16. \n", "\t
  17. 17
  18. \n", "\t
  19. 19
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 3\n", "\\item 5\n", "\\item 7\n", "\\item 9\n", "\\item 11\n", "\\item 13\n", "\\item 15\n", "\\item 17\n", "\\item 19\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 3\n", "3. 5\n", "4. 7\n", "5. 9\n", "6. 11\n", "7. 13\n", "8. 15\n", "9. 17\n", "10. 19\n", "\n", "\n" ], "text/plain": [ " [1] 1 3 5 7 9 11 13 15 17 19" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m=20\n", "odd=numeric()\n", "\n", "for (k in 1:m) {\n", " if (!k %% 2)\n", " next\n", " odd <- c(odd,k)\n", "}\n", "odd" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 3\n", "[1] 5\n", "[1] 7\n", "[1] 9\n" ] } ], "source": [ "for (i in 1:10) {\n", " if (!i %% 2){\n", " next\n", " }\n", " print(i)\n", "}" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"2 and so on. \"\n", "[1] \"3 and so on. \"\n", "[1] \"5 and so on. \"\n", "[1] \"6 and so on. \"\n", "[1] \"8 and so on. \"\n", "[1] \"9 and so on. \"\n" ] } ], "source": [ "x <- 1:10\n", "for (num in x){\n", " if (num %% 3 == 1) next\n", " mynum <- paste(num, \"and so on. \", sep = \" \")\n", " print(mynum)\n", "}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Switch Statment\n", "**switch statement allows a variable to be tested for equality against a list of values. Each value is called a case** \n", "\n", "Syntax:\n", "```r\n", "switch (Expression, \"Option 1\", \"Option 2\", \"Option 3\", ....., \"Option N\") \n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "subslide" } }, "source": [ "**Examples:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* If the value evaluated is a number, that item of the list is returned." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "'green'" ], "text/latex": [ "'green'" ], "text/markdown": [ "'green'" ], "text/plain": [ "[1] \"green\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "'red'" ], "text/latex": [ "'red'" ], "text/markdown": [ "'red'" ], "text/plain": [ "[1] \"red\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "'cloth'" ], "text/latex": [ "'cloth'" ], "text/markdown": [ "'cloth'" ], "text/plain": [ "[1] \"cloth\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "NULL" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "NULL" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "switch(2,\"red\",\"green\",\"blue\")\n", "# [1] \"green\"\n", "switch(1,\"red\",\"green\",\"blue\")\n", "# [1] \"red\"\n", "################\n", "x <- \"red\"\n", "switch(x, red=\"cloth\", size=5, name=\"table\")\n", "#################\n", "# If the numeric value is out of range (greater than the number of items in the list or smaller than 1), then, \n", "# NULL is returned.\n", " x <- switch(4,\"red\",\"green\",\"blue\")\n", " x\n", " x <- switch(0,\"red\",\"green\",\"blue\")\n", " x" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Please enter any ARITHMETIC OPERATOR You wish!: -\n", "[1] \"Subtraction of two numbers is: 10\"\n" ] } ], "source": [ "number1 <- 30\n", "number2 <- 20\n", "operator <- readline(prompt=\"Please enter any ARITHMETIC OPERATOR You wish!: \")\n", " \n", "switch(operator,\n", " \"+\" = print(paste(\"Addition of two numbers is: \", number1 + number2)),\n", " \"-\" = print(paste(\"Subtraction of two numbers is: \", number1 - number2)),\n", " \"*\" = print(paste(\"Multiplication of two numbers is: \", number1 * number2)),\n", " \"^\" = print(paste(\"Exponent of two numbers is: \", number1 ^ number2)),\n", " \"/\" = print(paste(\"Division of two numbers is: \", number1 / number2)),\n", " \"%/%\" = print(paste(\"Integer Division of two numbers is: \", number1 %/% number2)),\n", " \"%%\" = print(paste(\"Division of two numbers is: \", number1 %% number2))\n", ")" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "**Conclusion**\n", "\n", "**Loops are not recommended until and unless its really needed, since R has vectorisation feature**" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "run_control": { "frozen": false, "read_only": false }, "slideshow": { "slide_type": "subslide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 5 10 15 20 25 30 35 45\n", "[1] 6 7 8 9 10 11 12 14\n", "[1] -4 -3 -2 -1 0 1 2 4\n" ] } ], "source": [ "# let us take a vector \n", "vect <- c(1,2,3,4,5,6,7,9)\n", "# now we multiply each element of vect with 5\n", "print(vect * 5)\n", "# now we add each element of vect with 5\n", "print(vect + 5)\n", "# now we subtract each element of vect with 5\n", "print(vect - 5)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Vectorization and recycling concepts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Vectorized Operations**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* when you apply a function to a vector, it applies the function to every element of the vector.\n", "* All the mathematical operators, like +, -, *, and the logical operators, like & (AND), | (OR), and the comparison operators, like < and > are hungry to operate element-wise on every element of a vector." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "For example, to add pairs of numbers contained in two vectors" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 430
  2. \n", "\t
  3. 210
  4. \n", "\t
  5. 615
  6. \n", "\t
  7. -225
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 430\n", "\\item 210\n", "\\item 615\n", "\\item -225\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 430\n", "2. 210\n", "3. 615\n", "4. -225\n", "\n", "\n" ], "text/plain": [ "[1] 430 210 615 -225" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
    \n", "\t
  1. 33.9130434782609
  2. \n", "\t
  3. 91.304347826087
  4. \n", "\t
  5. 20.8695652173913
  6. \n", "\t
  7. 22.6086956521739
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 33.9130434782609\n", "\\item 91.304347826087\n", "\\item 20.8695652173913\n", "\\item 22.6086956521739\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 33.9130434782609\n", "2. 91.304347826087\n", "3. 20.8695652173913\n", "4. 22.6086956521739\n", "\n", "\n" ], "text/plain": [ "[1] 33.91304 91.30435 20.86957 22.60870" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
    \n", "\t
  1. 164
  2. \n", "\t
  3. 252
  4. \n", "\t
  5. 171
  6. \n", "\t
  7. 7
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 164\n", "\\item 252\n", "\\item 171\n", "\\item 7\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 164\n", "2. 252\n", "3. 171\n", "4. 7\n", "\n", "\n" ], "text/plain": [ "[1] 164 252 171 7" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
    \n", "\t
  1. 6708
  2. \n", "\t
  3. 8820
  4. \n", "\t
  5. 5904
  6. \n", "\t
  7. -2340
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 6708\n", "\\item 8820\n", "\\item 5904\n", "\\item -2340\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 6708\n", "2. 8820\n", "3. 5904\n", "4. -2340\n", "\n", "\n" ], "text/plain": [ "[1] 6708 8820 5904 -2340" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
    \n", "\t
  1. 8
  2. \n", "\t
  3. -168
  4. \n", "\t
  5. 75
  6. \n", "\t
  7. -97
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 8\n", "\\item -168\n", "\\item 75\n", "\\item -97\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 8\n", "2. -168\n", "3. 75\n", "4. -97\n", "\n", "\n" ], "text/plain": [ "[1] 8 -168 75 -97" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
    \n", "\t
  1. 1.1025641025641
  2. \n", "\t
  3. 0.2
  4. \n", "\t
  5. 2.5625
  6. \n", "\t
  7. -0.865384615384615
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1.1025641025641\n", "\\item 0.2\n", "\\item 2.5625\n", "\\item -0.865384615384615\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1.1025641025641\n", "2. 0.2\n", "3. 2.5625\n", "4. -0.865384615384615\n", "\n", "\n" ], "text/plain": [ "[1] 1.1025641 0.2000000 2.5625000 -0.8653846" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "first <- c(86, 42, 123, -45)\n", "second <- c(78, 210, 48, 52)\n", "first * 5 # with scalar\n", "second / 2.3\n", "first + second # vector with another vector\n", "first * second\n", "first - second\n", "first/second" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a <- 1:10\n", "b <- 1:10" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 2
  2. \n", "\t
  3. 4
  4. \n", "\t
  5. 6
  6. \n", "\t
  7. 8
  8. \n", "\t
  9. 10
  10. \n", "\t
  11. 12
  12. \n", "\t
  13. 14
  14. \n", "\t
  15. 16
  16. \n", "\t
  17. 18
  18. \n", "\t
  19. 20
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 2\n", "\\item 4\n", "\\item 6\n", "\\item 8\n", "\\item 10\n", "\\item 12\n", "\\item 14\n", "\\item 16\n", "\\item 18\n", "\\item 20\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 2\n", "2. 4\n", "3. 6\n", "4. 8\n", "5. 10\n", "6. 12\n", "7. 14\n", "8. 16\n", "9. 18\n", "10. 20\n", "\n", "\n" ], "text/plain": [ " [1] 2 4 6 8 10 12 14 16 18 20" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res <- numeric(length = length(a))\n", "for (i in seq_along(a)) {\n", " res[i] <- a[i] + b[i]\n", "}\n", "res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead, `+` is a vectorized function which can operate on entire vectors at once" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 2
  2. \n", "\t
  3. 4
  4. \n", "\t
  5. 6
  6. \n", "\t
  7. 8
  8. \n", "\t
  9. 10
  10. \n", "\t
  11. 12
  12. \n", "\t
  13. 14
  14. \n", "\t
  15. 16
  16. \n", "\t
  17. 18
  18. \n", "\t
  19. 20
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 2\n", "\\item 4\n", "\\item 6\n", "\\item 8\n", "\\item 10\n", "\\item 12\n", "\\item 14\n", "\\item 16\n", "\\item 18\n", "\\item 20\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 2\n", "2. 4\n", "3. 6\n", "4. 8\n", "5. 10\n", "6. 12\n", "7. 14\n", "8. 16\n", "9. 18\n", "10. 20\n", "\n", "\n" ], "text/plain": [ " [1] 2 4 6 8 10 12 14 16 18 20" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "res2 <- a + b\n", "res2\n", "all.equal(res, res2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Vector Recycling**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* R likes to operate on vectors of the same length\n", "* if it encounters two vectors of different lengths in a binary operation, it recycles the smaller vector until it is the same length as the longest vector.\n", "* It gives some warning, but still returns the output.\n", "* If you perform an operation on two or more vectors of unequal length, R will recycle elements of the shorter vector(s) to match the longest vector.\n", "* When R reaches the end of the shorter vector `b`, it starts again at the first element of `b` and continues until it reaches the last element of the longest vector `a`." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 2
  2. \n", "\t
  3. 4
  4. \n", "\t
  5. 6
  6. \n", "\t
  7. 8
  8. \n", "\t
  9. 10
  10. \n", "\t
  11. 7
  12. \n", "\t
  13. 9
  14. \n", "\t
  15. 11
  16. \n", "\t
  17. 13
  18. \n", "\t
  19. 15
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 2\n", "\\item 4\n", "\\item 6\n", "\\item 8\n", "\\item 10\n", "\\item 7\n", "\\item 9\n", "\\item 11\n", "\\item 13\n", "\\item 15\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 2\n", "2. 4\n", "3. 6\n", "4. 8\n", "5. 10\n", "6. 7\n", "7. 9\n", "8. 11\n", "9. 13\n", "10. 15\n", "\n", "\n" ], "text/plain": [ " [1] 2 4 6 8 10 7 9 11 13 15" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "a <- 1:10\n", "b <- 1:5\n", "a + b" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 5
  2. \n", "\t
  3. 10
  4. \n", "\t
  5. 15
  6. \n", "\t
  7. 20
  8. \n", "\t
  9. 25
  10. \n", "\t
  11. 30
  12. \n", "\t
  13. 35
  14. \n", "\t
  15. 40
  16. \n", "\t
  17. 45
  18. \n", "\t
  19. 50
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 5\n", "\\item 10\n", "\\item 15\n", "\\item 20\n", "\\item 25\n", "\\item 30\n", "\\item 35\n", "\\item 40\n", "\\item 45\n", "\\item 50\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 5\n", "2. 10\n", "3. 15\n", "4. 20\n", "5. 25\n", "6. 30\n", "7. 35\n", "8. 40\n", "9. 45\n", "10. 50\n", "\n", "\n" ], "text/plain": [ " [1] 5 10 15 20 25 30 35 40 45 50" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "a <- 1:10\n", "b <- 5\n", "a * b # here b is a vector of length 1 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* When the length of the longer object is a multiple of the shorter object length (as in our example above), the recycling occurs silently. When the longer object length is not a multiple of the shorter object length, a warning is given:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning message in a + b:\n", "\"longer object length is not a multiple of shorter object length\"" ] }, { "data": { "text/html": [ "
    \n", "\t
  1. 2
  2. \n", "\t
  3. 4
  4. \n", "\t
  5. 6
  6. \n", "\t
  7. 8
  8. \n", "\t
  9. 10
  10. \n", "\t
  11. 12
  12. \n", "\t
  13. 14
  14. \n", "\t
  15. 9
  16. \n", "\t
  17. 11
  18. \n", "\t
  19. 13
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 2\n", "\\item 4\n", "\\item 6\n", "\\item 8\n", "\\item 10\n", "\\item 12\n", "\\item 14\n", "\\item 9\n", "\\item 11\n", "\\item 13\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 2\n", "2. 4\n", "3. 6\n", "4. 8\n", "5. 10\n", "6. 12\n", "7. 14\n", "8. 9\n", "9. 11\n", "10. 13\n", "\n", "\n" ], "text/plain": [ " [1] 2 4 6 8 10 12 14 9 11 13" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "a <- 1:10\n", "b <- 1:7\n", "a + b" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\n", "
xy
1a
2b
3c
\n" ], "text/latex": [ "\\begin{tabular}{r|ll}\n", " x & y\\\\\n", "\\hline\n", "\t 1 & a\\\\\n", "\t 2 & b\\\\\n", "\t 3 & c\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "x | y | \n", "|---|---|---|\n", "| 1 | a | \n", "| 2 | b | \n", "| 3 | c | \n", "\n", "\n" ], "text/plain": [ " x y\n", "1 1 a\n", "2 2 b\n", "3 3 c" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ " x y 1\n", "1 1 a 1\n", "2 2 b 1\n", "3 3 c 1\n" ] } ], "source": [ "df <- data.frame(x = 1:3, y = c(\"a\", \"b\", \"c\"))\n", "df\n", "df1 <- cbind(df, 1) \n", "print(df1)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "notes" } }, "source": [ "Sources & References \n", "https://www.tutorialgateway.org/r-programming// \n", "https://swcarpentry.github.io/r-novice-inflammation/15-supp-loops-in-depth/ " ] } ], "metadata": { "celltoolbar": "Slideshow", "hide_input": false, "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.6.1" }, "nav_menu": {}, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": true, "toc_position": {}, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }