{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Solutions to Friday Morning Exercises\n", "\n", "These are exercises that we will do in the optional class on Friday morning (and possibly spill over into Monday morning) for those who want more practice with the basics of R programming. If you can do these coding challenges with little difficulty, there is no need to attend the Friday class. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 1**. Write a for loop over the numbers 1 to 10, and print out the square if the number is odd and the cube if the number is even. The output should look like this:\n", "\n", "```R\n", "[1] 1\n", "[1] 8\n", "[1] 9\n", "[1] 64\n", "[1] 25\n", "[1] 216\n", "[1] 49\n", "[1] 512\n", "[1] 81\n", "[1] 1000\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 8\n", "[1] 9\n", "[1] 64\n", "[1] 25\n", "[1] 216\n", "[1] 49\n", "[1] 512\n", "[1] 81\n", "[1] 1000\n" ] } ], "source": [ "for (i in 1:10) {\n", " if (i %% 2 == 0) {\n", " print(i^3)\n", " } else {\n", " print(i^2)\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 2**. A Pythagoran triplet is a set of intergers (x, y, z) such that $x^2 + y^2 = z^2$. For example, (3, 4, 5) is a Pythagorean triplet. Find all Pythogorean triplets where the value of z is less than or equql to 20, where the numbers (x, y, z) are in non-decreasing order. Your answer shouold look like this:\n", "\n", "```R\n", "[1] 3 4 5\n", "[1] 5 12 13\n", "[1] 6 8 10\n", "[1] 8 15 17\n", "[1] 9 12 15\n", "[1] 12 16 20\n", "```" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 3 4 5\n", "[1] 5 12 13\n", "[1] 6 8 10\n", "[1] 8 15 17\n", "[1] 9 12 15\n", "[1] 12 16 20\n" ] } ], "source": [ "for (x in 1:20) {\n", " for (y in x:20) {\n", " for (z in y:20) {\n", " if (x^2 + y^2 == z^2) {\n", " print(c(x, y, z))\n", " }\n", " }\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 3**. Find the position in numbers where the cumulative sum first exceeds 20. The answer should be\n", "```R\n", "[1] 34\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "set.seed(123)\n", "numbers <- runif(100)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 34\n" ] } ], "source": [ "s <- 0\n", "i <- 0\n", "while ((s <= 20) && (i <= length(numbers))) {\n", " i <- i + 1\n", " s <- s + numbers[i]\n", "}\n", "if (i > length(numbers)) {\n", " print(\"Not reached\")\n", "} else {\n", " print(i)\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 4**. Find the mean, standard deviation and sum of the odd numbers between 100 and 200. The answer should be:\n", "```R\n", "[1] 150\n", "[1] 29.15476\n", "[1] 7500\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 150.00000 29.15476 7500.00000\n" ] } ], "source": [ "ns <- seq(from=101, to=200, by=2)\n", "print(c(mean(ns), sd(ns), sum(ns)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ex 5**. Construct the following matrix and find its inverse.\n", "```\n", " [,1] [,2] [,3] [,4]\n", "[1,] 0 2 3 4\n", "[2,] 5 0 7 8\n", "[3,] 9 10 0 12\n", "[4,] 13 14 15 0\n", "```" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
0 2 3 4
5 0 7 8
910 012
131415 0
\n" ], "text/latex": [ "\\begin{tabular}{llll}\n", "\t 0 & 2 & 3 & 4\\\\\n", "\t 5 & 0 & 7 & 8\\\\\n", "\t 9 & 10 & 0 & 12\\\\\n", "\t 13 & 14 & 15 & 0\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| 0 | 2 | 3 | 4 | \n", "| 5 | 0 | 7 | 8 | \n", "| 9 | 10 | 0 | 12 | \n", "| 13 | 14 | 15 | 0 | \n", "\n", "\n" ], "text/plain": [ " [,1] [,2] [,3] [,4]\n", "[1,] 0 2 3 4 \n", "[2,] 5 0 7 8 \n", "[3,] 9 10 0 12 \n", "[4,] 13 14 15 0 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m <- matrix(1:16, nrow=4, byrow=T)\n", "diag(m) = 0\n", "m" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
-0.29522863 0.09244533 0.03677932 0.01590457
0.15805169-0.12524851 0.03081511 0.02683897
0.10834990 0.03677932-0.06063618 0.02783300
0.08971173 0.03503976 0.03006958-0.03429423
\n" ], "text/latex": [ "\\begin{tabular}{llll}\n", "\t -0.29522863 & 0.09244533 & 0.03677932 & 0.01590457\\\\\n", "\t 0.15805169 & -0.12524851 & 0.03081511 & 0.02683897\\\\\n", "\t 0.10834990 & 0.03677932 & -0.06063618 & 0.02783300\\\\\n", "\t 0.08971173 & 0.03503976 & 0.03006958 & -0.03429423\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| -0.29522863 | 0.09244533 | 0.03677932 | 0.01590457 | \n", "| 0.15805169 | -0.12524851 | 0.03081511 | 0.02683897 | \n", "| 0.10834990 | 0.03677932 | -0.06063618 | 0.02783300 | \n", "| 0.08971173 | 0.03503976 | 0.03006958 | -0.03429423 | \n", "\n", "\n" ], "text/plain": [ " [,1] [,2] [,3] [,4] \n", "[1,] -0.29522863 0.09244533 0.03677932 0.01590457\n", "[2,] 0.15805169 -0.12524851 0.03081511 0.02683897\n", "[3,] 0.10834990 0.03677932 -0.06063618 0.02783300\n", "[4,] 0.08971173 0.03503976 0.03006958 -0.03429423" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "solve(m)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**6**. Simulate 10 rolls of a six-sided die. Count the number of die rols less than 3. Doo not use a for or while loop. Your solution will look like this:\n", "```R\n", "[1] 6 2 1 6 5 1 4 6 4 3\n", "[2] 3\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 4
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 6
  8. \n", "\t
  9. 3
  10. \n", "\t
  11. 6
  12. \n", "\t
  13. 6
  14. \n", "\t
  15. 4
  16. \n", "\t
  17. 3
  18. \n", "\t
  19. 1
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 4\n", "\\item 2\n", "\\item 3\n", "\\item 6\n", "\\item 3\n", "\\item 6\n", "\\item 6\n", "\\item 4\n", "\\item 3\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 4\n", "2. 2\n", "3. 3\n", "4. 6\n", "5. 3\n", "6. 6\n", "7. 6\n", "8. 4\n", "9. 3\n", "10. 1\n", "\n", "\n" ], "text/plain": [ " [1] 4 2 3 6 3 6 6 4 3 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "(rolls <- sample(1:6, 10, replace=T))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "2" ], "text/latex": [ "2" ], "text/markdown": [ "2" ], "text/plain": [ "[1] 2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sum(rolls < 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**7**. Write a function called `peek` with signature `peek(m, n)` where m is a matrix and n is an integer. If n is greater than the number of rows of m, return m. Otherwise return n random rows from m (without repetition).\n", "\n", "For example, if m is\n", "```R\n", " [,1] [,2]\n", " [1,] 1 11\n", " [2,] 2 12\n", " [3,] 3 13\n", " [4,] 4 14\n", " [5,] 5 15\n", " [6,] 6 16\n", " [7,] 7 17\n", " [8,] 8 18\n", " [9,] 9 19\n", "[10,] 10 20\n", "```\n", "peek(m, 5) might be\n", "```R\n", " [,1] [,2]\n", "[1,] 10 20\n", "[2,] 2 12\n", "[3,] 1 11\n", "[4,] 8 18\n", "[5,] 5 15\n", "```" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "peek <- function(m, n) {\n", " if (n > nrow(m)) {\n", " return(m)\n", " } else {\n", " idx = sample(1:nrow(m), n)\n", " return(m[idx,])\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
1020
313
111
717
515
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t 10 & 20\\\\\n", "\t 3 & 13\\\\\n", "\t 1 & 11\\\\\n", "\t 7 & 17\\\\\n", "\t 5 & 15\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| 10 | 20 | \n", "| 3 | 13 | \n", "| 1 | 11 | \n", "| 7 | 17 | \n", "| 5 | 15 | \n", "\n", "\n" ], "text/plain": [ " [,1] [,2]\n", "[1,] 10 20 \n", "[2,] 3 13 \n", "[3,] 1 11 \n", "[4,] 7 17 \n", "[5,] 5 15 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m <- matrix(1:20, ncol=2)\n", "peek(m, 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**8**. Create a matrix m that has the following values\n", "```R\n", " [,1] [,2]\n", " [1,] 1 11\n", " [2,] 2 12\n", " [3,] 3 13\n", " [4,] 4 14\n", " [5,] 5 15\n", " [6,] 6 16\n", " [7,] 7 17\n", " [8,] 8 18\n", " [9,] 9 19\n", "[10,] 10 20\n", "```\n", "Print the row means, row sums, column means and column sums. Normalize the column values so that the column means are 0 are the column standard deviations are 1. You can do this by subtracting the column mean from each column, then dividing by the oclumn standard deviation." ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "m <- matrix(1:20, ncol=2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Row means" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 6
  2. \n", "\t
  3. 7
  4. \n", "\t
  5. 8
  6. \n", "\t
  7. 9
  8. \n", "\t
  9. 10
  10. \n", "\t
  11. 11
  12. \n", "\t
  13. 12
  14. \n", "\t
  15. 13
  16. \n", "\t
  17. 14
  18. \n", "\t
  19. 15
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 6\n", "\\item 7\n", "\\item 8\n", "\\item 9\n", "\\item 10\n", "\\item 11\n", "\\item 12\n", "\\item 13\n", "\\item 14\n", "\\item 15\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 6\n", "2. 7\n", "3. 8\n", "4. 9\n", "5. 10\n", "6. 11\n", "7. 12\n", "8. 13\n", "9. 14\n", "10. 15\n", "\n", "\n" ], "text/plain": [ " [1] 6 7 8 9 10 11 12 13 14 15" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "rowMeans(m)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 6
  2. \n", "\t
  3. 7
  4. \n", "\t
  5. 8
  6. \n", "\t
  7. 9
  8. \n", "\t
  9. 10
  10. \n", "\t
  11. 11
  12. \n", "\t
  13. 12
  14. \n", "\t
  15. 13
  16. \n", "\t
  17. 14
  18. \n", "\t
  19. 15
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 6\n", "\\item 7\n", "\\item 8\n", "\\item 9\n", "\\item 10\n", "\\item 11\n", "\\item 12\n", "\\item 13\n", "\\item 14\n", "\\item 15\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 6\n", "2. 7\n", "3. 8\n", "4. 9\n", "5. 10\n", "6. 11\n", "7. 12\n", "8. 13\n", "9. 14\n", "10. 15\n", "\n", "\n" ], "text/plain": [ " [1] 6 7 8 9 10 11 12 13 14 15" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "apply(m, 1, mean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Col means" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 5.5
  2. \n", "\t
  3. 15.5
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 5.5\n", "\\item 15.5\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 5.5\n", "2. 15.5\n", "\n", "\n" ], "text/plain": [ "[1] 5.5 15.5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "colMeans(m)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 5.5
  2. \n", "\t
  3. 15.5
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 5.5\n", "\\item 15.5\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 5.5\n", "2. 15.5\n", "\n", "\n" ], "text/plain": [ "[1] 5.5 15.5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "apply(m, 2, mean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Row and column sums" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 12
  2. \n", "\t
  3. 14
  4. \n", "\t
  5. 16
  6. \n", "\t
  7. 18
  8. \n", "\t
  9. 20
  10. \n", "\t
  11. 22
  12. \n", "\t
  13. 24
  14. \n", "\t
  15. 26
  16. \n", "\t
  17. 28
  18. \n", "\t
  19. 30
  20. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 12\n", "\\item 14\n", "\\item 16\n", "\\item 18\n", "\\item 20\n", "\\item 22\n", "\\item 24\n", "\\item 26\n", "\\item 28\n", "\\item 30\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 12\n", "2. 14\n", "3. 16\n", "4. 18\n", "5. 20\n", "6. 22\n", "7. 24\n", "8. 26\n", "9. 28\n", "10. 30\n", "\n", "\n" ], "text/plain": [ " [1] 12 14 16 18 20 22 24 26 28 30" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "apply(m, 1, sum)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 55
  2. \n", "\t
  3. 155
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 55\n", "\\item 155\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 55\n", "2. 155\n", "\n", "\n" ], "text/plain": [ "[1] 55 155" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "apply(m, 2, sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Scaling" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
-1.4863011-1.4863011
-1.1560120-1.1560120
-0.8257228-0.8257228
-0.4954337-0.4954337
-0.1651446-0.1651446
0.1651446 0.1651446
0.4954337 0.4954337
0.8257228 0.8257228
1.1560120 1.1560120
1.4863011 1.4863011
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t -1.4863011 & -1.4863011\\\\\n", "\t -1.1560120 & -1.1560120\\\\\n", "\t -0.8257228 & -0.8257228\\\\\n", "\t -0.4954337 & -0.4954337\\\\\n", "\t -0.1651446 & -0.1651446\\\\\n", "\t 0.1651446 & 0.1651446\\\\\n", "\t 0.4954337 & 0.4954337\\\\\n", "\t 0.8257228 & 0.8257228\\\\\n", "\t 1.1560120 & 1.1560120\\\\\n", "\t 1.4863011 & 1.4863011\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| -1.4863011 | -1.4863011 | \n", "| -1.1560120 | -1.1560120 | \n", "| -0.8257228 | -0.8257228 | \n", "| -0.4954337 | -0.4954337 | \n", "| -0.1651446 | -0.1651446 | \n", "| 0.1651446 | 0.1651446 | \n", "| 0.4954337 | 0.4954337 | \n", "| 0.8257228 | 0.8257228 | \n", "| 1.1560120 | 1.1560120 | \n", "| 1.4863011 | 1.4863011 | \n", "\n", "\n" ], "text/plain": [ " [,1] [,2] \n", " [1,] -1.4863011 -1.4863011\n", " [2,] -1.1560120 -1.1560120\n", " [3,] -0.8257228 -0.8257228\n", " [4,] -0.4954337 -0.4954337\n", " [5,] -0.1651446 -0.1651446\n", " [6,] 0.1651446 0.1651446\n", " [7,] 0.4954337 0.4954337\n", " [8,] 0.8257228 0.8257228\n", " [9,] 1.1560120 1.1560120\n", "[10,] 1.4863011 1.4863011" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "scale(m)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
-1.4863011 1.8165902
-4.4589032-1.1560120
-0.8257228 2.4771685
-3.7983250-0.4954337
-0.1651446 3.1377467
-3.1377467 0.1651446
0.4954337 3.7983250
-2.4771685 0.8257228
1.1560120 4.4589032
-1.8165902 1.4863011
\n" ], "text/latex": [ "\\begin{tabular}{ll}\n", "\t -1.4863011 & 1.8165902\\\\\n", "\t -4.4589032 & -1.1560120\\\\\n", "\t -0.8257228 & 2.4771685\\\\\n", "\t -3.7983250 & -0.4954337\\\\\n", "\t -0.1651446 & 3.1377467\\\\\n", "\t -3.1377467 & 0.1651446\\\\\n", "\t 0.4954337 & 3.7983250\\\\\n", "\t -2.4771685 & 0.8257228\\\\\n", "\t 1.1560120 & 4.4589032\\\\\n", "\t -1.8165902 & 1.4863011\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| -1.4863011 | 1.8165902 | \n", "| -4.4589032 | -1.1560120 | \n", "| -0.8257228 | 2.4771685 | \n", "| -3.7983250 | -0.4954337 | \n", "| -0.1651446 | 3.1377467 | \n", "| -3.1377467 | 0.1651446 | \n", "| 0.4954337 | 3.7983250 | \n", "| -2.4771685 | 0.8257228 | \n", "| 1.1560120 | 4.4589032 | \n", "| -1.8165902 | 1.4863011 | \n", "\n", "\n" ], "text/plain": [ " [,1] [,2] \n", " [1,] -1.4863011 1.8165902\n", " [2,] -4.4589032 -1.1560120\n", " [3,] -0.8257228 2.4771685\n", " [4,] -3.7983250 -0.4954337\n", " [5,] -0.1651446 3.1377467\n", " [6,] -3.1377467 0.1651446\n", " [7,] 0.4954337 3.7983250\n", " [8,] -2.4771685 0.8257228\n", " [9,] 1.1560120 4.4589032\n", "[10,] -1.8165902 1.4863011" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mu <- apply(m, 2, mean)\n", "sigma <- apply(m, 2, sd)\n", "(m - mu)/sigma" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.4.0" } }, "nbformat": 4, "nbformat_minor": 2 }