{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "height <- c(58,59,60,61,62,63,64,65,66, 67, 68, 69, 70, 71, 72)\n", "weight <- c(115,117,120,123,126,129,132,135,139, 142, 146, 150, 154, 159, 164)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "htwtmatrix = matrix(c(height,weight),15,2) # what do 15 and 2 refer to?" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [,1] [,2]\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\n", "[11,] 68 146\n", "[12,] 69 150\n", "[13,] 70 154\n", "[14,] 71 159\n", "[15,] 72 164\n" ] }, { "data": { "text/html": [ "
    \n", "\t
  1. 15
  2. \n", "\t
  3. 2
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 15\n", "\\item 2\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 15\n", "2. 2\n", "\n", "\n" ], "text/plain": [ "[1] 15 2" ] }, "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": [ "print(htwtmatrix)\n", "dim(htwtmatrix)\n", "is.array(htwtmatrix) \n", "# even its a matrix it will return TRUE, because matrix also a type of array in 2 dimentional \n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'height'
  2. \n", "\t
  3. 'weight'
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'height'\n", "\\item 'weight'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'height'\n", "2. 'weight'\n", "\n", "\n" ], "text/plain": [ "[1] \"height\" \"weight\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# to assign names for each column first convert it into a dataframe\n", "htwtdata = data.frame(htwtmatrix) # as.dataframe is also works well here\n", "names(htwtdata) = c(\"height\", \"weight\")\n", "# here we used names() function to assign the names for our dataframe\n", "names(htwtdata) # here we are extracting the names , names function can be used to set or get the names\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "hide_input": false, "run_control": { "frozen": false, "read_only": false }, "scrolled": true }, "outputs": [], "source": [ "# Let us see how R operates on matrices, and how that compares to data frames\n", "htwtmatrix * 2 # multiplying with 2 gives us result of multiplication on each value \n", "htwtmatrix[, 1]/12 # convert height in inches to feet\n", "mean(htwtmatrix[, 2]) # find mean of weight" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 15
  2. \n", "\t
  3. 2
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 15\n", "\\item 2\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 15\n", "2. 2\n", "\n", "\n" ], "text/plain": [ "[1] 15 2" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "15" ], "text/latex": [ "15" ], "text/markdown": [ "15" ], "text/plain": [ "[1] 15" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "'data.frame':\t15 obs. of 2 variables:\n", " $ height: num 58 59 60 61 62 63 64 65 66 67 ...\n", " $ weight: num 115 117 120 123 126 129 132 135 139 142 ...\n" ] }, { "data": { "text/plain": [ " height weight \n", " Min. :58.0 Min. :115.0 \n", " 1st Qu.:61.5 1st Qu.:124.5 \n", " Median :65.0 Median :135.0 \n", " Mean :65.0 Mean :136.7 \n", " 3rd Qu.:68.5 3rd Qu.:148.0 \n", " Max. :72.0 Max. :164.0 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dim(htwtdata)\n", "nrow(htwtdata)\n", "str(htwtdata)\n", "summary(htwtdata)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 24.032401902497
  2. \n", "\t
  3. 23.6285550129273
  4. \n", "\t
  5. 23.4333333333333
  6. \n", "\t
  7. 23.2381080354743
  8. \n", "\t
  9. 23.0431841831426
  10. \n", "\t
  11. 22.848828420257
  12. \n", "\t
  13. 22.6552734375
  14. \n", "\t
  15. 22.4627218934911
  16. \n", "\t
  17. 22.4327364554637
  18. \n", "\t
  19. 22.2379149030965
  20. \n", "\t
  21. 22.1967993079585
  22. \n", "\t
  23. 22.1487082545684
  24. \n", "\t
  25. 22.0942857142857
  26. \n", "\t
  27. 22.1735766712954
  28. \n", "\t
  29. 22.2399691358025
  30. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 24.032401902497\n", "\\item 23.6285550129273\n", "\\item 23.4333333333333\n", "\\item 23.2381080354743\n", "\\item 23.0431841831426\n", "\\item 22.848828420257\n", "\\item 22.6552734375\n", "\\item 22.4627218934911\n", "\\item 22.4327364554637\n", "\\item 22.2379149030965\n", "\\item 22.1967993079585\n", "\\item 22.1487082545684\n", "\\item 22.0942857142857\n", "\\item 22.1735766712954\n", "\\item 22.2399691358025\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 24.032401902497\n", "2. 23.6285550129273\n", "3. 23.4333333333333\n", "4. 23.2381080354743\n", "5. 23.0431841831426\n", "6. 22.848828420257\n", "7. 22.6552734375\n", "8. 22.4627218934911\n", "9. 22.4327364554637\n", "10. 22.2379149030965\n", "11. 22.1967993079585\n", "12. 22.1487082545684\n", "13. 22.0942857142857\n", "14. 22.1735766712954\n", "15. 22.2399691358025\n", "\n", "\n" ], "text/plain": [ " [1] 24.03240 23.62856 23.43333 23.23811 23.04318 22.84883 22.65527 22.46272\n", " [9] 22.43274 22.23791 22.19680 22.14871 22.09429 22.17358 22.23997" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "htwtdata[,2]*703/htwtdata[,1]^2" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'height'
  2. \n", "\t
  3. 'weight'
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'height'\n", "\\item 'weight'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'height'\n", "2. 'weight'\n", "\n", "\n" ], "text/plain": [ "[1] \"height\" \"weight\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "65" ], "text/latex": [ "65" ], "text/markdown": [ "65" ], "text/plain": [ "[1] 65" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "135" ], "text/latex": [ "135" ], "text/markdown": [ "135" ], "text/plain": [ "[1] 135" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "67" ], "text/latex": [ "67" ], "text/markdown": [ "67" ], "text/plain": [ "[1] 67" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "142" ], "text/latex": [ "142" ], "text/markdown": [ "142" ], "text/plain": [ "[1] 142" ] }, "metadata": {}, "output_type": "display_data" }, { "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" } ], "source": [ "# How would you get R to give you the height and weight of the 8th student in the\n", "# data set? The 8th and 10th student?\n", "names(htwtdata)\n", "htwtdata[8,1] # 8th student height\n", "htwtdata[8,2] #1 0th student height\n", "htwtdata[10,1] # 10th student height \n", "htwtdata[10,2] # 10th student weight\n", "head(htwtdata,10) #check with head command" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Loops \n", "## If/else statements\n", "\n", "In R, one can write a conditional statement as follows: \n", "\n", "```R\n", "ifelse(condition on data, true value returned, false returned)\n", "```\n", " \n", "\n", "\n", "The above expression reads: if condition on the data is true, then do the true value assigned; otherwise execute the \"false value.\"\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "6" ], "text/latex": [ "6" ], "text/markdown": [ "6" ], "text/plain": [ "[1] 6" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[1] 6\n" ] }, { "data": { "text/html": [ "5" ], "text/latex": [ "5" ], "text/markdown": [ "5" ], "text/plain": [ "[1] 5" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[1] 5\n" ] } ], "source": [ "ifelse(3 > 4, x <- 5, x <- 6)\n", "print(x)\n", "\n", "ifelse(4 > 3, x <- 5, x <- 6)\n", "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Usage of operators like & & , | | , &, |" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mean height= 65 \n", " mean weight= 136.7333" ] } ], "source": [ "hmean = mean(htwtdata$height)\n", "wmean = mean(htwtdata$weight)\n", "?cat \n", "cat(\"mean height=\",hmean,\"\\n\",\"mean weight=\",wmean)\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The operators && and || are often used to denote multiple conditions in an if statement. Whereas &(and) and |(or) apply element-wise to vectors, && and || apply to vectors of length one, and only evaluate their second argument in the sequence if necessary. Thus it is important to remember which logical operator to use in which situation. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "5" ], "text/latex": [ "5" ], "text/markdown": [ "5" ], "text/plain": [ "[1] 5" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "ifelse( hmean > 61 && wmean > 120, x <- 5, x <- 6) # multiple conditions in an if statement" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [1] \"low\" \"low\" \"low\" \"low\" \"low\" \"low\" \"low\" \"low\" \"low\" \"low\" \n", "[11] \"low\" \"low\" \"high\" \"high\" \"high\"\n" ] }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "htwt_cat<-ifelse (height>=70 | weight>159, \"high\", \"low\") # apply element-wise to vectors\n", "print(htwt_cat)\n", "is.vector(htwt_cat)\n" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
heightweighthtwt_cat[c(1:3, c(13:15))]
158 115 low
259 117 low
360 120 low
1370 154 high
1471 159 high
1572 164 high
\n" ], "text/latex": [ "\\begin{tabular}{r|lll}\n", " & height & weight & htwt\\_cat{[}c(1:3, c(13:15)){]}\\\\\n", "\\hline\n", "\t1 & 58 & 115 & low \\\\\n", "\t2 & 59 & 117 & low \\\\\n", "\t3 & 60 & 120 & low \\\\\n", "\t13 & 70 & 154 & high\\\\\n", "\t14 & 71 & 159 & high\\\\\n", "\t15 & 72 & 164 & high\\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | height | weight | htwt_cat[c(1:3, c(13:15))] | \n", "|---|---|---|---|---|---|\n", "| 1 | 58 | 115 | low | \n", "| 2 | 59 | 117 | low | \n", "| 3 | 60 | 120 | low | \n", "| 13 | 70 | 154 | high | \n", "| 14 | 71 | 159 | high | \n", "| 15 | 72 | 164 | high | \n", "\n", "\n" ], "text/plain": [ " height weight htwt_cat[c(1:3, c(13:15))]\n", "1 58 115 low \n", "2 59 117 low \n", "3 60 120 low \n", "13 70 154 high \n", "14 71 159 high \n", "15 72 164 high " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#htwtdata1<-head(htwtdata,6)\n", "cbind(htwtdata[c(1:3,c(13:15)),],htwt_cat[c(1:3,c(13:15))])\n", "#print(htwtdata1[1:3])" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'low'
  2. \n", "\t
  3. 'low'
  4. \n", "\t
  5. 'low'
  6. \n", "\t
  7. 'low'
  8. \n", "\t
  9. 'low'
  10. \n", "\t
  11. 'low'
  12. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'low'\n", "\\item 'low'\n", "\\item 'low'\n", "\\item 'low'\n", "\\item 'low'\n", "\\item 'low'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'low'\n", "2. 'low'\n", "3. 'low'\n", "4. 'low'\n", "5. 'low'\n", "6. 'low'\n", "\n", "\n" ], "text/plain": [ "[1] \"low\" \"low\" \"low\" \"low\" \"low\" \"low\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "htwt_cat[1:6]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "'low'" ], "text/latex": [ "'low'" ], "text/markdown": [ "'low'" ], "text/plain": [ "[1] \"low\"" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "'high'" ], "text/latex": [ "'high'" ], "text/markdown": [ "'high'" ], "text/plain": [ "[1] \"high\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "htwt_cat <- ifelse(height > 67 || weight > 150, \"high\", \"low\")\n", "htwt_cat \n", "# Notice that in the above ifelse statement only the first element in the series was computed.\n", "htwt_cat <- ifelse(height > 57 || weight > 110, \"high\", \"low\")\n", "htwt_cat" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'fail'
  2. \n", "\t
  3. 'fail'
  4. \n", "\t
  5. 'pass'
  6. \n", "\t
  7. 'pass'
  8. \n", "\t
  9. 'pass'
  10. \n", "\t
  11. 'pass'
  12. \n", "\t
  13. 'pass'
  14. \n", "\t
  15. 'pass'
  16. \n", "\t
  17. 'pass'
  18. \n", "\t
  19. 'pass'
  20. \n", "\t
  21. 'pass'
  22. \n", "\t
  23. 'pass'
  24. \n", "\t
  25. 'pass'
  26. \n", "\t
  27. 'pass'
  28. \n", "\t
  29. 'pass'
  30. \n", "\t
  31. 'pass'
  32. \n", "\t
  33. 'pass'
  34. \n", "\t
  35. 'pass'
  36. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'fail'\n", "\\item 'fail'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\item 'pass'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'fail'\n", "2. 'fail'\n", "3. 'pass'\n", "4. 'pass'\n", "5. 'pass'\n", "6. 'pass'\n", "7. 'pass'\n", "8. 'pass'\n", "9. 'pass'\n", "10. 'pass'\n", "11. 'pass'\n", "12. 'pass'\n", "13. 'pass'\n", "14. 'pass'\n", "15. 'pass'\n", "16. 'pass'\n", "17. 'pass'\n", "18. 'pass'\n", "\n", "\n" ], "text/plain": [ " [1] \"fail\" \"fail\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\"\n", "[11] \"pass\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\" \"pass\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#This can also be extended to include multiple conditions. Suppose we have the following data:\n", "\n", "final_score<- c(39, 51, 60, 65, 72, 78, 79, 83, 85, 85, 87, 89, 91, 95, 96, 97, 100, 100)\n", "\n", "passfail<-ifelse(final_score>=60, \"pass\", \"fail\")\n", "passfail" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Suppose we want to create a variable called grades that is assigned as follows:\n", "```R \n", "\"F\" if final_score <60\n", "\n", "\"D\" if 60≤final_score<70\n", "\n", "\"C\" if 70≤final_score<80\n", "\n", "\"B\" if 80≤final_score<90\n", "\n", "\"A\" if 90≤final_score \n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nested ifelse Statements\n", "\n", "We can use a \"nested\" ifelse command as follows:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'F'
  2. \n", "\t
  3. 'F'
  4. \n", "\t
  5. 'D'
  6. \n", "\t
  7. 'D'
  8. \n", "\t
  9. 'C'
  10. \n", "\t
  11. 'C'
  12. \n", "\t
  13. 'C'
  14. \n", "\t
  15. 'B'
  16. \n", "\t
  17. 'B'
  18. \n", "\t
  19. 'B'
  20. \n", "\t
  21. 'B'
  22. \n", "\t
  23. 'B'
  24. \n", "\t
  25. 'A'
  26. \n", "\t
  27. 'A'
  28. \n", "\t
  29. 'A'
  30. \n", "\t
  31. 'A'
  32. \n", "\t
  33. 'A'
  34. \n", "\t
  35. 'A'
  36. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'F'\n", "\\item 'F'\n", "\\item 'D'\n", "\\item 'D'\n", "\\item 'C'\n", "\\item 'C'\n", "\\item 'C'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'B'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'A'\n", "\\item 'A'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'F'\n", "2. 'F'\n", "3. 'D'\n", "4. 'D'\n", "5. 'C'\n", "6. 'C'\n", "7. 'C'\n", "8. 'B'\n", "9. 'B'\n", "10. 'B'\n", "11. 'B'\n", "12. 'B'\n", "13. 'A'\n", "14. 'A'\n", "15. 'A'\n", "16. 'A'\n", "17. 'A'\n", "18. 'A'\n", "\n", "\n" ], "text/plain": [ " [1] \"F\" \"F\" \"D\" \"D\" \"C\" \"C\" \"C\" \"B\" \"B\" \"B\" \"B\" \"B\" \"A\" \"A\" \"A\" \"A\" \"A\" \"A\"" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "grade <- ifelse(final_score < 60, \"F\", ifelse(final_score < 70, \"D\", ifelse(final_score < \n", " 80, \"C\", ifelse(final_score < 90, \"B\", \"A\"))))\n", "\n", "grade" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The logic by which this will assign grades is depicted in the figure below.\n", "\n", "![Image of NestedIf](https://drive.google.com/uc?id=0B14DjwBCvz91d1k2a0l5X3ZHVDg)" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "#if we want to resize the image we can use the below code \n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Repetitive Execution: \"for\" loops, \"repeat\" and \"while\"\n", "\n", "syntax of for loop is: \n", "```R\n", "for (name in expr_1) expr_2\n", "```\n", "here name is the loop variable, expr_1 is a vector expression, (often a sequence like 1:20), and expr_2 is often a grouped expression with its sub-expressions written in terms of the dummy name. expr_2 is repeatedly evaluated as name ranges through the values in the vector result of expr_1. \n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "# let's take airquality dataset which is the Daily air quality measurements in\n", "# New York, May to September 1973. for the details use \n", "?airquality" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 153\n" ] } ], "source": [ "# we want to figure out which days were good air quality days (1) or bad air\n", "# quality (0), based on a cutoff of ozone levels above 60ppb.\n", "numdays <- nrow(airquality)\n", "print(numdays)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false, "hide_input": false, "run_control": { "frozen": false, "read_only": false }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", " [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", " [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "[112] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "[149] 0 0 0 0 0\n" ] } ], "source": [ "# creates an object which will store the vector\n", "goodair <- numeric(numdays)\n", "print(goodair)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```R\n", "for(i in 1:numdays)\n", " if (airquality$Ozone[i] > 60) goodair[i] = 0 else goodair[i] = 1\n", " ```\n", "##(Notice that we have an if statement here within a for loop.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "#Does the command above work? Why/why not?\n", "#Let's check the Ozone variable. What do you notice below?\n", "airquality$Ozone" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "# When there are missing values, many operations in R fail. One way to get around\n", "# this is to create a new data frame that deletes all the rows corresponding to\n", "# observations with missing rows. This can be done by means of the command\n", "# 'na.omit'\n", "airqualfull = na.omit(airquality)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 111
  2. \n", "\t
  3. 6
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 111\n", "\\item 6\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 111\n", "2. 6\n", "\n", "\n" ], "text/plain": [ "[1] 111 6" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
    \n", "\t
  1. 153
  2. \n", "\t
  3. 6
  4. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 153\n", "\\item 6\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 153\n", "2. 6\n", "\n", "\n" ], "text/plain": [ "[1] 153 6" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "dim(airqualfull)\n", "dim(airquality) " ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "111" ], "text/latex": [ "111" ], "text/markdown": [ "111" ], "text/plain": [ "[1] 111" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[1] 111\n" ] } ], "source": [ "#Now let's try doing this again with the data with the complete cases.\n", "numdays = nrow(airqualfull)\n", "numdays\n", "print(numdays)\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "goodair = numeric(numdays) # initialize the vector" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "for(i in 1:numdays)\n", "\n", " if (airqualfull$Ozone[i] >60) goodair[i] = 0 else goodair[i] = 1" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 1
  4. \n", "\t
  5. 1
  6. \n", "\t
  7. 1
  8. \n", "\t
  9. 1
  10. \n", "\t
  11. 1
  12. \n", "\t
  13. 1
  14. \n", "\t
  15. 1
  16. \n", "\t
  17. 1
  18. \n", "\t
  19. 1
  20. \n", "\t
  21. 1
  22. \n", "\t
  23. 1
  24. \n", "\t
  25. 1
  26. \n", "\t
  27. 1
  28. \n", "\t
  29. 1
  30. \n", "\t
  31. 1
  32. \n", "\t
  33. 1
  34. \n", "\t
  35. 1
  36. \n", "\t
  37. 1
  38. \n", "\t
  39. 1
  40. \n", "\t
  41. 1
  42. \n", "\t
  43. 1
  44. \n", "\t
  45. 0
  46. \n", "\t
  47. 1
  48. \n", "\t
  49. 1
  50. \n", "\t
  51. 0
  52. \n", "\t
  53. 1
  54. \n", "\t
  55. 1
  56. \n", "\t
  57. 1
  58. \n", "\t
  59. 1
  60. \n", "\t
  61. 1
  62. \n", "\t
  63. 1
  64. \n", "\t
  65. 1
  66. \n", "\t
  67. 0
  68. \n", "\t
  69. 1
  70. \n", "\t
  71. 1
  72. \n", "\t
  73. 0
  74. \n", "\t
  75. 1
  76. \n", "\t
  77. 0
  78. \n", "\t
  79. 0
  80. \n", "\t
  81. 0
  82. \n", "\t
  83. 0
  84. \n", "\t
  85. 1
  86. \n", "\t
  87. 1
  88. \n", "\t
  89. 1
  90. \n", "\t
  91. 1
  92. \n", "\t
  93. 1
  94. \n", "\t
  95. 0
  96. \n", "\t
  97. 0
  98. \n", "\t
  99. 0
  100. \n", "\t
  101. 1
  102. \n", "\t
  103. 0
  104. \n", "\t
  105. 0
  106. \n", "\t
  107. 1
  108. \n", "\t
  109. 1
  110. \n", "\t
  111. 0
  112. \n", "\t
  113. 1
  114. \n", "\t
  115. 0
  116. \n", "\t
  117. 1
  118. \n", "\t
  119. 1
  120. \n", "\t
  121. 1
  122. \n", "\t
  123. 1
  124. \n", "\t
  125. 0
  126. \n", "\t
  127. 0
  128. \n", "\t
  129. 0
  130. \n", "\t
  131. 1
  132. \n", "\t
  133. 1
  134. \n", "\t
  135. 0
  136. \n", "\t
  137. 1
  138. \n", "\t
  139. 1
  140. \n", "\t
  141. 1
  142. \n", "\t
  143. 1
  144. \n", "\t
  145. 1
  146. \n", "\t
  147. 1
  148. \n", "\t
  149. 1
  150. \n", "\t
  151. 1
  152. \n", "\t
  153. 0
  154. \n", "\t
  155. 0
  156. \n", "\t
  157. 0
  158. \n", "\t
  159. 0
  160. \n", "\t
  161. 0
  162. \n", "\t
  163. 0
  164. \n", "\t
  165. 0
  166. \n", "\t
  167. 0
  168. \n", "\t
  169. 0
  170. \n", "\t
  171. 0
  172. \n", "\t
  173. 1
  174. \n", "\t
  175. 1
  176. \n", "\t
  177. 1
  178. \n", "\t
  179. 1
  180. \n", "\t
  181. 1
  182. \n", "\t
  183. 1
  184. \n", "\t
  185. 1
  186. \n", "\t
  187. 1
  188. \n", "\t
  189. 1
  190. \n", "\t
  191. 1
  192. \n", "\t
  193. 1
  194. \n", "\t
  195. 1
  196. \n", "\t
  197. 1
  198. \n", "\t
  199. 1
  200. \n", "\t
  201. 1
  202. \n", "\t
  203. 1
  204. \n", "\t
  205. 1
  206. \n", "\t
  207. 1
  208. \n", "\t
  209. 1
  210. \n", "\t
  211. 1
  212. \n", "\t
  213. 1
  214. \n", "\t
  215. 1
  216. \n", "\t
  217. 1
  218. \n", "\t
  219. 1
  220. \n", "\t
  221. 1
  222. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\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 0\n", "\\item 0\n", "\\item 0\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\item 1\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 1\n", "3. 1\n", "4. 1\n", "5. 1\n", "6. 1\n", "7. 1\n", "8. 1\n", "9. 1\n", "10. 1\n", "11. 1\n", "12. 1\n", "13. 1\n", "14. 1\n", "15. 1\n", "16. 1\n", "17. 1\n", "18. 1\n", "19. 1\n", "20. 1\n", "21. 1\n", "22. 1\n", "23. 0\n", "24. 1\n", "25. 1\n", "26. 0\n", "27. 1\n", "28. 1\n", "29. 1\n", "30. 1\n", "31. 1\n", "32. 1\n", "33. 1\n", "34. 0\n", "35. 1\n", "36. 1\n", "37. 0\n", "38. 1\n", "39. 0\n", "40. 0\n", "41. 0\n", "42. 0\n", "43. 1\n", "44. 1\n", "45. 1\n", "46. 1\n", "47. 1\n", "48. 0\n", "49. 0\n", "50. 0\n", "51. 1\n", "52. 0\n", "53. 0\n", "54. 1\n", "55. 1\n", "56. 0\n", "57. 1\n", "58. 0\n", "59. 1\n", "60. 1\n", "61. 1\n", "62. 1\n", "63. 0\n", "64. 0\n", "65. 0\n", "66. 1\n", "67. 1\n", "68. 0\n", "69. 1\n", "70. 1\n", "71. 1\n", "72. 1\n", "73. 1\n", "74. 1\n", "75. 1\n", "76. 1\n", "77. 0\n", "78. 0\n", "79. 0\n", "80. 0\n", "81. 0\n", "82. 0\n", "83. 0\n", "84. 0\n", "85. 0\n", "86. 0\n", "87. 1\n", "88. 1\n", "89. 1\n", "90. 1\n", "91. 1\n", "92. 1\n", "93. 1\n", "94. 1\n", "95. 1\n", "96. 1\n", "97. 1\n", "98. 1\n", "99. 1\n", "100. 1\n", "101. 1\n", "102. 1\n", "103. 1\n", "104. 1\n", "105. 1\n", "106. 1\n", "107. 1\n", "108. 1\n", "109. 1\n", "110. 1\n", "111. 1\n", "\n", "\n" ], "text/plain": [ " [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 0 1 1 0\n", " [38] 1 0 0 0 0 1 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 1 1\n", " [75] 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "goodair" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 1
  2. \n", "\t
  3. 2
  4. \n", "\t
  5. 3
  6. \n", "\t
  7. 4
  8. \n", "\t
  9. 5
  10. \n", "\t
  11. 6
  12. \n", "\t
  13. 7
  14. \n", "\t
  15. 8
  16. \n", "\t
  17. 9
  18. \n", "\t
  19. 10
  20. \n", "\t
  21. 11
  22. \n", "\t
  23. 12
  24. \n", "\t
  25. 13
  26. \n", "\t
  27. 14
  28. \n", "\t
  29. 15
  30. \n", "\t
  31. 16
  32. \n", "\t
  33. 17
  34. \n", "\t
  35. 18
  36. \n", "\t
  37. 19
  38. \n", "\t
  39. 20
  40. \n", "\t
  41. 21
  42. \n", "\t
  43. 22
  44. \n", "\t
  45. 24
  46. \n", "\t
  47. 25
  48. \n", "\t
  49. 27
  50. \n", "\t
  51. 28
  52. \n", "\t
  53. 29
  54. \n", "\t
  55. 30
  56. \n", "\t
  57. 31
  58. \n", "\t
  59. 32
  60. \n", "\t
  61. 33
  62. \n", "\t
  63. 35
  64. \n", "\t
  65. 36
  66. \n", "\t
  67. 38
  68. \n", "\t
  69. 43
  70. \n", "\t
  71. 44
  72. \n", "\t
  73. 45
  74. \n", "\t
  75. 46
  76. \n", "\t
  77. 47
  78. \n", "\t
  79. 51
  80. \n", "\t
  81. 54
  82. \n", "\t
  83. 55
  84. \n", "\t
  85. 57
  86. \n", "\t
  87. 59
  88. \n", "\t
  89. 60
  90. \n", "\t
  91. 61
  92. \n", "\t
  93. 62
  94. \n", "\t
  95. 66
  96. \n", "\t
  97. 67
  98. \n", "\t
  99. 69
  100. \n", "\t
  101. 70
  102. \n", "\t
  103. 71
  104. \n", "\t
  105. 72
  106. \n", "\t
  107. 73
  108. \n", "\t
  109. 74
  110. \n", "\t
  111. 75
  112. \n", "\t
  113. 76
  114. \n", "\t
  115. 87
  116. \n", "\t
  117. 88
  118. \n", "\t
  119. 89
  120. \n", "\t
  121. 90
  122. \n", "\t
  123. 91
  124. \n", "\t
  125. 92
  126. \n", "\t
  127. 93
  128. \n", "\t
  129. 94
  130. \n", "\t
  131. 95
  132. \n", "\t
  133. 96
  134. \n", "\t
  135. 97
  136. \n", "\t
  137. 98
  138. \n", "\t
  139. 99
  140. \n", "\t
  141. 100
  142. \n", "\t
  143. 101
  144. \n", "\t
  145. 102
  146. \n", "\t
  147. 103
  148. \n", "\t
  149. 104
  150. \n", "\t
  151. 105
  152. \n", "\t
  153. 106
  154. \n", "\t
  155. 107
  156. \n", "\t
  157. 108
  158. \n", "\t
  159. 109
  160. \n", "\t
  161. 110
  162. \n", "\t
  163. 111
  164. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 1\n", "\\item 2\n", "\\item 3\n", "\\item 4\n", "\\item 5\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", "\\item 16\n", "\\item 17\n", "\\item 18\n", "\\item 19\n", "\\item 20\n", "\\item 21\n", "\\item 22\n", "\\item 24\n", "\\item 25\n", "\\item 27\n", "\\item 28\n", "\\item 29\n", "\\item 30\n", "\\item 31\n", "\\item 32\n", "\\item 33\n", "\\item 35\n", "\\item 36\n", "\\item 38\n", "\\item 43\n", "\\item 44\n", "\\item 45\n", "\\item 46\n", "\\item 47\n", "\\item 51\n", "\\item 54\n", "\\item 55\n", "\\item 57\n", "\\item 59\n", "\\item 60\n", "\\item 61\n", "\\item 62\n", "\\item 66\n", "\\item 67\n", "\\item 69\n", "\\item 70\n", "\\item 71\n", "\\item 72\n", "\\item 73\n", "\\item 74\n", "\\item 75\n", "\\item 76\n", "\\item 87\n", "\\item 88\n", "\\item 89\n", "\\item 90\n", "\\item 91\n", "\\item 92\n", "\\item 93\n", "\\item 94\n", "\\item 95\n", "\\item 96\n", "\\item 97\n", "\\item 98\n", "\\item 99\n", "\\item 100\n", "\\item 101\n", "\\item 102\n", "\\item 103\n", "\\item 104\n", "\\item 105\n", "\\item 106\n", "\\item 107\n", "\\item 108\n", "\\item 109\n", "\\item 110\n", "\\item 111\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 1\n", "2. 2\n", "3. 3\n", "4. 4\n", "5. 5\n", "6. 6\n", "7. 7\n", "8. 8\n", "9. 9\n", "10. 10\n", "11. 11\n", "12. 12\n", "13. 13\n", "14. 14\n", "15. 15\n", "16. 16\n", "17. 17\n", "18. 18\n", "19. 19\n", "20. 20\n", "21. 21\n", "22. 22\n", "23. 24\n", "24. 25\n", "25. 27\n", "26. 28\n", "27. 29\n", "28. 30\n", "29. 31\n", "30. 32\n", "31. 33\n", "32. 35\n", "33. 36\n", "34. 38\n", "35. 43\n", "36. 44\n", "37. 45\n", "38. 46\n", "39. 47\n", "40. 51\n", "41. 54\n", "42. 55\n", "43. 57\n", "44. 59\n", "45. 60\n", "46. 61\n", "47. 62\n", "48. 66\n", "49. 67\n", "50. 69\n", "51. 70\n", "52. 71\n", "53. 72\n", "54. 73\n", "55. 74\n", "56. 75\n", "57. 76\n", "58. 87\n", "59. 88\n", "60. 89\n", "61. 90\n", "62. 91\n", "63. 92\n", "64. 93\n", "65. 94\n", "66. 95\n", "67. 96\n", "68. 97\n", "69. 98\n", "70. 99\n", "71. 100\n", "72. 101\n", "73. 102\n", "74. 103\n", "75. 104\n", "76. 105\n", "77. 106\n", "78. 107\n", "79. 108\n", "80. 109\n", "81. 110\n", "82. 111\n", "\n", "\n" ], "text/plain": [ " [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n", "[20] 20 21 22 24 25 27 28 29 30 31 32 33 35 36 38 43 44 45 46\n", "[39] 47 51 54 55 57 59 60 61 62 66 67 69 70 71 72 73 74 75 76\n", "[58] 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105\n", "[77] 106 107 108 109 110 111" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# At this point we might be interested in which days were the ones with good air\n", "# quality. The 'which' command returns a set of indices corresponding to the\n", "# condition specified. We can then use the indices to find the day of the month\n", "# this corresponds to\n", "which(goodair == 1) ## notice the double \"=\" signs!" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false }, "scrolled": true }, "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", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
OzoneSolar.RWindTempMonthDay
141 190 7.467 5 1
236 118 8.072 5 2
312 149 12.674 5 3
418 313 11.562 5 4
723 299 8.665 5 7
819 99 13.859 5 8
9 8 19 20.161 5 9
1216 256 9.769 5 12
1311 290 9.266 5 13
1414 274 10.968 5 14
1518 65 13.258 5 15
1614 334 11.564 5 16
1734 307 12.066 5 17
18 6 78 18.457 5 18
1930 322 11.568 5 19
2011 44 9.762 5 20
21 1 8 9.759 5 21
2211 320 16.673 5 22
23 4 25 9.761 5 23
2432 92 12.061 5 24
2823 13 12.067 5 28
2945 252 14.981 5 29
3137 279 7.476 5 31
3829 127 9.782 6 7
4139 323 11.587 6 10
4423 148 8.082 6 13
4721 191 14.977 6 16
4837 284 20.772 6 17
4920 37 9.265 6 18
5012 120 11.573 6 19
.....................
11131 244 10.978 8 19
11244 190 10.378 8 20
11321 259 15.577 8 21
114 9 36 14.372 8 22
11645 212 9.779 8 24
12847 95 7.487 9 5
12932 92 15.584 9 6
13020 252 10.980 9 7
13123 220 10.378 9 8
13221 230 10.975 9 9
13324 259 9.773 9 10
13444 236 14.981 9 11
13521 259 15.576 9 12
13628 238 6.377 9 13
137 9 24 10.971 9 14
13813 112 11.571 9 15
13946 237 6.978 9 16
14018 224 13.867 9 17
14113 27 10.376 9 18
14224 238 10.368 9 19
14316 201 8.082 9 20
14413 238 12.664 9 21
14523 14 9.271 9 22
14636 139 10.381 9 23
147 7 49 10.369 9 24
14814 20 16.663 9 25
14930 193 6.970 9 26
15114 191 14.375 9 28
15218 131 8.076 9 29
15320 223 11.568 9 30
\n" ], "text/latex": [ "\\begin{tabular}{r|llllll}\n", " & Ozone & Solar.R & Wind & Temp & Month & Day\\\\\n", "\\hline\n", "\t1 & 41 & 190 & 7.4 & 67 & 5 & 1 \\\\\n", "\t2 & 36 & 118 & 8.0 & 72 & 5 & 2 \\\\\n", "\t3 & 12 & 149 & 12.6 & 74 & 5 & 3 \\\\\n", "\t4 & 18 & 313 & 11.5 & 62 & 5 & 4 \\\\\n", "\t7 & 23 & 299 & 8.6 & 65 & 5 & 7 \\\\\n", "\t8 & 19 & 99 & 13.8 & 59 & 5 & 8 \\\\\n", "\t9 & 8 & 19 & 20.1 & 61 & 5 & 9 \\\\\n", "\t12 & 16 & 256 & 9.7 & 69 & 5 & 12 \\\\\n", "\t13 & 11 & 290 & 9.2 & 66 & 5 & 13 \\\\\n", "\t14 & 14 & 274 & 10.9 & 68 & 5 & 14 \\\\\n", "\t15 & 18 & 65 & 13.2 & 58 & 5 & 15 \\\\\n", "\t16 & 14 & 334 & 11.5 & 64 & 5 & 16 \\\\\n", "\t17 & 34 & 307 & 12.0 & 66 & 5 & 17 \\\\\n", "\t18 & 6 & 78 & 18.4 & 57 & 5 & 18 \\\\\n", "\t19 & 30 & 322 & 11.5 & 68 & 5 & 19 \\\\\n", "\t20 & 11 & 44 & 9.7 & 62 & 5 & 20 \\\\\n", "\t21 & 1 & 8 & 9.7 & 59 & 5 & 21 \\\\\n", "\t22 & 11 & 320 & 16.6 & 73 & 5 & 22 \\\\\n", "\t23 & 4 & 25 & 9.7 & 61 & 5 & 23 \\\\\n", "\t24 & 32 & 92 & 12.0 & 61 & 5 & 24 \\\\\n", "\t28 & 23 & 13 & 12.0 & 67 & 5 & 28 \\\\\n", "\t29 & 45 & 252 & 14.9 & 81 & 5 & 29 \\\\\n", "\t31 & 37 & 279 & 7.4 & 76 & 5 & 31 \\\\\n", "\t38 & 29 & 127 & 9.7 & 82 & 6 & 7 \\\\\n", "\t41 & 39 & 323 & 11.5 & 87 & 6 & 10 \\\\\n", "\t44 & 23 & 148 & 8.0 & 82 & 6 & 13 \\\\\n", "\t47 & 21 & 191 & 14.9 & 77 & 6 & 16 \\\\\n", "\t48 & 37 & 284 & 20.7 & 72 & 6 & 17 \\\\\n", "\t49 & 20 & 37 & 9.2 & 65 & 6 & 18 \\\\\n", "\t50 & 12 & 120 & 11.5 & 73 & 6 & 19 \\\\\n", "\t... & ... & ... & ... & ... & ... & ...\\\\\n", "\t111 & 31 & 244 & 10.9 & 78 & 8 & 19 \\\\\n", "\t112 & 44 & 190 & 10.3 & 78 & 8 & 20 \\\\\n", "\t113 & 21 & 259 & 15.5 & 77 & 8 & 21 \\\\\n", "\t114 & 9 & 36 & 14.3 & 72 & 8 & 22 \\\\\n", "\t116 & 45 & 212 & 9.7 & 79 & 8 & 24 \\\\\n", "\t128 & 47 & 95 & 7.4 & 87 & 9 & 5 \\\\\n", "\t129 & 32 & 92 & 15.5 & 84 & 9 & 6 \\\\\n", "\t130 & 20 & 252 & 10.9 & 80 & 9 & 7 \\\\\n", "\t131 & 23 & 220 & 10.3 & 78 & 9 & 8 \\\\\n", "\t132 & 21 & 230 & 10.9 & 75 & 9 & 9 \\\\\n", "\t133 & 24 & 259 & 9.7 & 73 & 9 & 10 \\\\\n", "\t134 & 44 & 236 & 14.9 & 81 & 9 & 11 \\\\\n", "\t135 & 21 & 259 & 15.5 & 76 & 9 & 12 \\\\\n", "\t136 & 28 & 238 & 6.3 & 77 & 9 & 13 \\\\\n", "\t137 & 9 & 24 & 10.9 & 71 & 9 & 14 \\\\\n", "\t138 & 13 & 112 & 11.5 & 71 & 9 & 15 \\\\\n", "\t139 & 46 & 237 & 6.9 & 78 & 9 & 16 \\\\\n", "\t140 & 18 & 224 & 13.8 & 67 & 9 & 17 \\\\\n", "\t141 & 13 & 27 & 10.3 & 76 & 9 & 18 \\\\\n", "\t142 & 24 & 238 & 10.3 & 68 & 9 & 19 \\\\\n", "\t143 & 16 & 201 & 8.0 & 82 & 9 & 20 \\\\\n", "\t144 & 13 & 238 & 12.6 & 64 & 9 & 21 \\\\\n", "\t145 & 23 & 14 & 9.2 & 71 & 9 & 22 \\\\\n", "\t146 & 36 & 139 & 10.3 & 81 & 9 & 23 \\\\\n", "\t147 & 7 & 49 & 10.3 & 69 & 9 & 24 \\\\\n", "\t148 & 14 & 20 & 16.6 & 63 & 9 & 25 \\\\\n", "\t149 & 30 & 193 & 6.9 & 70 & 9 & 26 \\\\\n", "\t151 & 14 & 191 & 14.3 & 75 & 9 & 28 \\\\\n", "\t152 & 18 & 131 & 8.0 & 76 & 9 & 29 \\\\\n", "\t153 & 20 & 223 & 11.5 & 68 & 9 & 30 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | Ozone | Solar.R | Wind | Temp | Month | Day | \n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| 1 | 41 | 190 | 7.4 | 67 | 5 | 1 | \n", "| 2 | 36 | 118 | 8.0 | 72 | 5 | 2 | \n", "| 3 | 12 | 149 | 12.6 | 74 | 5 | 3 | \n", "| 4 | 18 | 313 | 11.5 | 62 | 5 | 4 | \n", "| 7 | 23 | 299 | 8.6 | 65 | 5 | 7 | \n", "| 8 | 19 | 99 | 13.8 | 59 | 5 | 8 | \n", "| 9 | 8 | 19 | 20.1 | 61 | 5 | 9 | \n", "| 12 | 16 | 256 | 9.7 | 69 | 5 | 12 | \n", "| 13 | 11 | 290 | 9.2 | 66 | 5 | 13 | \n", "| 14 | 14 | 274 | 10.9 | 68 | 5 | 14 | \n", "| 15 | 18 | 65 | 13.2 | 58 | 5 | 15 | \n", "| 16 | 14 | 334 | 11.5 | 64 | 5 | 16 | \n", "| 17 | 34 | 307 | 12.0 | 66 | 5 | 17 | \n", "| 18 | 6 | 78 | 18.4 | 57 | 5 | 18 | \n", "| 19 | 30 | 322 | 11.5 | 68 | 5 | 19 | \n", "| 20 | 11 | 44 | 9.7 | 62 | 5 | 20 | \n", "| 21 | 1 | 8 | 9.7 | 59 | 5 | 21 | \n", "| 22 | 11 | 320 | 16.6 | 73 | 5 | 22 | \n", "| 23 | 4 | 25 | 9.7 | 61 | 5 | 23 | \n", "| 24 | 32 | 92 | 12.0 | 61 | 5 | 24 | \n", "| 28 | 23 | 13 | 12.0 | 67 | 5 | 28 | \n", "| 29 | 45 | 252 | 14.9 | 81 | 5 | 29 | \n", "| 31 | 37 | 279 | 7.4 | 76 | 5 | 31 | \n", "| 38 | 29 | 127 | 9.7 | 82 | 6 | 7 | \n", "| 41 | 39 | 323 | 11.5 | 87 | 6 | 10 | \n", "| 44 | 23 | 148 | 8.0 | 82 | 6 | 13 | \n", "| 47 | 21 | 191 | 14.9 | 77 | 6 | 16 | \n", "| 48 | 37 | 284 | 20.7 | 72 | 6 | 17 | \n", "| 49 | 20 | 37 | 9.2 | 65 | 6 | 18 | \n", "| 50 | 12 | 120 | 11.5 | 73 | 6 | 19 | \n", "| ... | ... | ... | ... | ... | ... | ... | \n", "| 111 | 31 | 244 | 10.9 | 78 | 8 | 19 | \n", "| 112 | 44 | 190 | 10.3 | 78 | 8 | 20 | \n", "| 113 | 21 | 259 | 15.5 | 77 | 8 | 21 | \n", "| 114 | 9 | 36 | 14.3 | 72 | 8 | 22 | \n", "| 116 | 45 | 212 | 9.7 | 79 | 8 | 24 | \n", "| 128 | 47 | 95 | 7.4 | 87 | 9 | 5 | \n", "| 129 | 32 | 92 | 15.5 | 84 | 9 | 6 | \n", "| 130 | 20 | 252 | 10.9 | 80 | 9 | 7 | \n", "| 131 | 23 | 220 | 10.3 | 78 | 9 | 8 | \n", "| 132 | 21 | 230 | 10.9 | 75 | 9 | 9 | \n", "| 133 | 24 | 259 | 9.7 | 73 | 9 | 10 | \n", "| 134 | 44 | 236 | 14.9 | 81 | 9 | 11 | \n", "| 135 | 21 | 259 | 15.5 | 76 | 9 | 12 | \n", "| 136 | 28 | 238 | 6.3 | 77 | 9 | 13 | \n", "| 137 | 9 | 24 | 10.9 | 71 | 9 | 14 | \n", "| 138 | 13 | 112 | 11.5 | 71 | 9 | 15 | \n", "| 139 | 46 | 237 | 6.9 | 78 | 9 | 16 | \n", "| 140 | 18 | 224 | 13.8 | 67 | 9 | 17 | \n", "| 141 | 13 | 27 | 10.3 | 76 | 9 | 18 | \n", "| 142 | 24 | 238 | 10.3 | 68 | 9 | 19 | \n", "| 143 | 16 | 201 | 8.0 | 82 | 9 | 20 | \n", "| 144 | 13 | 238 | 12.6 | 64 | 9 | 21 | \n", "| 145 | 23 | 14 | 9.2 | 71 | 9 | 22 | \n", "| 146 | 36 | 139 | 10.3 | 81 | 9 | 23 | \n", "| 147 | 7 | 49 | 10.3 | 69 | 9 | 24 | \n", "| 148 | 14 | 20 | 16.6 | 63 | 9 | 25 | \n", "| 149 | 30 | 193 | 6.9 | 70 | 9 | 26 | \n", "| 151 | 14 | 191 | 14.3 | 75 | 9 | 28 | \n", "| 152 | 18 | 131 | 8.0 | 76 | 9 | 29 | \n", "| 153 | 20 | 223 | 11.5 | 68 | 9 | 30 | \n", "\n", "\n" ], "text/plain": [ " Ozone Solar.R Wind Temp Month Day\n", "1 41 190 7.4 67 5 1 \n", "2 36 118 8.0 72 5 2 \n", "3 12 149 12.6 74 5 3 \n", "4 18 313 11.5 62 5 4 \n", "7 23 299 8.6 65 5 7 \n", "8 19 99 13.8 59 5 8 \n", "9 8 19 20.1 61 5 9 \n", "12 16 256 9.7 69 5 12 \n", "13 11 290 9.2 66 5 13 \n", "14 14 274 10.9 68 5 14 \n", "15 18 65 13.2 58 5 15 \n", "16 14 334 11.5 64 5 16 \n", "17 34 307 12.0 66 5 17 \n", "18 6 78 18.4 57 5 18 \n", "19 30 322 11.5 68 5 19 \n", "20 11 44 9.7 62 5 20 \n", "21 1 8 9.7 59 5 21 \n", "22 11 320 16.6 73 5 22 \n", "23 4 25 9.7 61 5 23 \n", "24 32 92 12.0 61 5 24 \n", "28 23 13 12.0 67 5 28 \n", "29 45 252 14.9 81 5 29 \n", "31 37 279 7.4 76 5 31 \n", "38 29 127 9.7 82 6 7 \n", "41 39 323 11.5 87 6 10 \n", "44 23 148 8.0 82 6 13 \n", "47 21 191 14.9 77 6 16 \n", "48 37 284 20.7 72 6 17 \n", "49 20 37 9.2 65 6 18 \n", "50 12 120 11.5 73 6 19 \n", "... ... ... ... ... ... ...\n", "111 31 244 10.9 78 8 19 \n", "112 44 190 10.3 78 8 20 \n", "113 21 259 15.5 77 8 21 \n", "114 9 36 14.3 72 8 22 \n", "116 45 212 9.7 79 8 24 \n", "128 47 95 7.4 87 9 5 \n", "129 32 92 15.5 84 9 6 \n", "130 20 252 10.9 80 9 7 \n", "131 23 220 10.3 78 9 8 \n", "132 21 230 10.9 75 9 9 \n", "133 24 259 9.7 73 9 10 \n", "134 44 236 14.9 81 9 11 \n", "135 21 259 15.5 76 9 12 \n", "136 28 238 6.3 77 9 13 \n", "137 9 24 10.9 71 9 14 \n", "138 13 112 11.5 71 9 15 \n", "139 46 237 6.9 78 9 16 \n", "140 18 224 13.8 67 9 17 \n", "141 13 27 10.3 76 9 18 \n", "142 24 238 10.3 68 9 19 \n", "143 16 201 8.0 82 9 20 \n", "144 13 238 12.6 64 9 21 \n", "145 23 14 9.2 71 9 22 \n", "146 36 139 10.3 81 9 23 \n", "147 7 49 10.3 69 9 24 \n", "148 14 20 16.6 63 9 25 \n", "149 30 193 6.9 70 9 26 \n", "151 14 191 14.3 75 9 28 \n", "152 18 131 8.0 76 9 29 \n", "153 20 223 11.5 68 9 30 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "goodindices <- which(goodair == 1)\n", "airqualfull[goodindices,] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Suppose we want to define a day with good quality air as one with ozone levels below 60ppb, and temperatures less than 80 degrees F. Write an R loop to do this, and output the resulting subset of the data to a file called goodquality.txt. (Hint: use an ifelse() statement inside the for loop.)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 67
  2. \n", "\t
  3. 72
  4. \n", "\t
  5. 74
  6. \n", "\t
  7. 62
  8. \n", "\t
  9. 56
  10. \n", "\t
  11. 66
  12. \n", "\t
  13. 65
  14. \n", "\t
  15. 59
  16. \n", "\t
  17. 61
  18. \n", "\t
  19. 69
  20. \n", "\t
  21. 74
  22. \n", "\t
  23. 69
  24. \n", "\t
  25. 66
  26. \n", "\t
  27. 68
  28. \n", "\t
  29. 58
  30. \n", "\t
  31. 64
  32. \n", "\t
  33. 66
  34. \n", "\t
  35. 57
  36. \n", "\t
  37. 68
  38. \n", "\t
  39. 62
  40. \n", "\t
  41. 59
  42. \n", "\t
  43. 73
  44. \n", "\t
  45. 61
  46. \n", "\t
  47. 61
  48. \n", "\t
  49. 57
  50. \n", "\t
  51. 58
  52. \n", "\t
  53. 57
  54. \n", "\t
  55. 67
  56. \n", "\t
  57. 81
  58. \n", "\t
  59. 79
  60. \n", "\t
  61. 76
  62. \n", "\t
  63. 78
  64. \n", "\t
  65. 74
  66. \n", "\t
  67. 67
  68. \n", "\t
  69. 84
  70. \n", "\t
  71. 85
  72. \n", "\t
  73. 79
  74. \n", "\t
  75. 82
  76. \n", "\t
  77. 87
  78. \n", "\t
  79. 90
  80. \n", "\t
  81. 87
  82. \n", "\t
  83. 93
  84. \n", "\t
  85. 92
  86. \n", "\t
  87. 82
  88. \n", "\t
  89. 80
  90. \n", "\t
  91. 79
  92. \n", "\t
  93. 77
  94. \n", "\t
  95. 72
  96. \n", "\t
  97. 65
  98. \n", "\t
  99. 73
  100. \n", "\t
  101. 76
  102. \n", "\t
  103. 77
  104. \n", "\t
  105. 76
  106. \n", "\t
  107. 76
  108. \n", "\t
  109. 76
  110. \n", "\t
  111. 75
  112. \n", "\t
  113. 78
  114. \n", "\t
  115. 73
  116. \n", "\t
  117. 80
  118. \n", "\t
  119. 77
  120. \n", "\t
  121. 83
  122. \n", "\t
  123. 84
  124. \n", "\t
  125. 85
  126. \n", "\t
  127. 81
  128. \n", "\t
  129. 84
  130. \n", "\t
  131. 83
  132. \n", "\t
  133. 83
  134. \n", "\t
  135. 88
  136. \n", "\t
  137. 92
  138. \n", "\t
  139. 92
  140. \n", "\t
  141. 89
  142. \n", "\t
  143. 82
  144. \n", "\t
  145. 73
  146. \n", "\t
  147. 81
  148. \n", "\t
  149. 91
  150. \n", "\t
  151. 80
  152. \n", "\t
  153. 81
  154. \n", "\t
  155. 82
  156. \n", "\t
  157. 84
  158. \n", "\t
  159. 87
  160. \n", "\t
  161. 85
  162. \n", "\t
  163. 74
  164. \n", "\t
  165. 81
  166. \n", "\t
  167. 82
  168. \n", "\t
  169. 86
  170. \n", "\t
  171. 85
  172. \n", "\t
  173. 82
  174. \n", "\t
  175. 86
  176. \n", "\t
  177. 88
  178. \n", "\t
  179. 86
  180. \n", "\t
  181. 83
  182. \n", "\t
  183. 81
  184. \n", "\t
  185. 81
  186. \n", "\t
  187. 81
  188. \n", "\t
  189. 82
  190. \n", "\t
  191. 86
  192. \n", "\t
  193. 85
  194. \n", "\t
  195. 87
  196. \n", "\t
  197. 89
  198. \n", "\t
  199. 90
  200. \n", "\t
  201. 90
  202. \n", "\t
  203. 92
  204. \n", "\t
  205. 86
  206. \n", "\t
  207. 86
  208. \n", "\t
  209. 82
  210. \n", "\t
  211. 80
  212. \n", "\t
  213. 79
  214. \n", "\t
  215. 77
  216. \n", "\t
  217. 79
  218. \n", "\t
  219. 76
  220. \n", "\t
  221. 78
  222. \n", "\t
  223. 78
  224. \n", "\t
  225. 77
  226. \n", "\t
  227. 72
  228. \n", "\t
  229. 75
  230. \n", "\t
  231. 79
  232. \n", "\t
  233. 81
  234. \n", "\t
  235. 86
  236. \n", "\t
  237. 88
  238. \n", "\t
  239. 97
  240. \n", "\t
  241. 94
  242. \n", "\t
  243. 96
  244. \n", "\t
  245. 94
  246. \n", "\t
  247. 91
  248. \n", "\t
  249. 92
  250. \n", "\t
  251. 93
  252. \n", "\t
  253. 93
  254. \n", "\t
  255. 87
  256. \n", "\t
  257. 84
  258. \n", "\t
  259. 80
  260. \n", "\t
  261. 78
  262. \n", "\t
  263. 75
  264. \n", "\t
  265. 73
  266. \n", "\t
  267. 81
  268. \n", "\t
  269. 76
  270. \n", "\t
  271. 77
  272. \n", "\t
  273. 71
  274. \n", "\t
  275. 71
  276. \n", "\t
  277. 78
  278. \n", "\t
  279. 67
  280. \n", "\t
  281. 76
  282. \n", "\t
  283. 68
  284. \n", "\t
  285. 82
  286. \n", "\t
  287. 64
  288. \n", "\t
  289. 71
  290. \n", "\t
  291. 81
  292. \n", "\t
  293. 69
  294. \n", "\t
  295. 63
  296. \n", "\t
  297. 70
  298. \n", "\t
  299. 77
  300. \n", "\t
  301. 75
  302. \n", "\t
  303. 76
  304. \n", "\t
  305. 68
  306. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 67\n", "\\item 72\n", "\\item 74\n", "\\item 62\n", "\\item 56\n", "\\item 66\n", "\\item 65\n", "\\item 59\n", "\\item 61\n", "\\item 69\n", "\\item 74\n", "\\item 69\n", "\\item 66\n", "\\item 68\n", "\\item 58\n", "\\item 64\n", "\\item 66\n", "\\item 57\n", "\\item 68\n", "\\item 62\n", "\\item 59\n", "\\item 73\n", "\\item 61\n", "\\item 61\n", "\\item 57\n", "\\item 58\n", "\\item 57\n", "\\item 67\n", "\\item 81\n", "\\item 79\n", "\\item 76\n", "\\item 78\n", "\\item 74\n", "\\item 67\n", "\\item 84\n", "\\item 85\n", "\\item 79\n", "\\item 82\n", "\\item 87\n", "\\item 90\n", "\\item 87\n", "\\item 93\n", "\\item 92\n", "\\item 82\n", "\\item 80\n", "\\item 79\n", "\\item 77\n", "\\item 72\n", "\\item 65\n", "\\item 73\n", "\\item 76\n", "\\item 77\n", "\\item 76\n", "\\item 76\n", "\\item 76\n", "\\item 75\n", "\\item 78\n", "\\item 73\n", "\\item 80\n", "\\item 77\n", "\\item 83\n", "\\item 84\n", "\\item 85\n", "\\item 81\n", "\\item 84\n", "\\item 83\n", "\\item 83\n", "\\item 88\n", "\\item 92\n", "\\item 92\n", "\\item 89\n", "\\item 82\n", "\\item 73\n", "\\item 81\n", "\\item 91\n", "\\item 80\n", "\\item 81\n", "\\item 82\n", "\\item 84\n", "\\item 87\n", "\\item 85\n", "\\item 74\n", "\\item 81\n", "\\item 82\n", "\\item 86\n", "\\item 85\n", "\\item 82\n", "\\item 86\n", "\\item 88\n", "\\item 86\n", "\\item 83\n", "\\item 81\n", "\\item 81\n", "\\item 81\n", "\\item 82\n", "\\item 86\n", "\\item 85\n", "\\item 87\n", "\\item 89\n", "\\item 90\n", "\\item 90\n", "\\item 92\n", "\\item 86\n", "\\item 86\n", "\\item 82\n", "\\item 80\n", "\\item 79\n", "\\item 77\n", "\\item 79\n", "\\item 76\n", "\\item 78\n", "\\item 78\n", "\\item 77\n", "\\item 72\n", "\\item 75\n", "\\item 79\n", "\\item 81\n", "\\item 86\n", "\\item 88\n", "\\item 97\n", "\\item 94\n", "\\item 96\n", "\\item 94\n", "\\item 91\n", "\\item 92\n", "\\item 93\n", "\\item 93\n", "\\item 87\n", "\\item 84\n", "\\item 80\n", "\\item 78\n", "\\item 75\n", "\\item 73\n", "\\item 81\n", "\\item 76\n", "\\item 77\n", "\\item 71\n", "\\item 71\n", "\\item 78\n", "\\item 67\n", "\\item 76\n", "\\item 68\n", "\\item 82\n", "\\item 64\n", "\\item 71\n", "\\item 81\n", "\\item 69\n", "\\item 63\n", "\\item 70\n", "\\item 77\n", "\\item 75\n", "\\item 76\n", "\\item 68\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 67\n", "2. 72\n", "3. 74\n", "4. 62\n", "5. 56\n", "6. 66\n", "7. 65\n", "8. 59\n", "9. 61\n", "10. 69\n", "11. 74\n", "12. 69\n", "13. 66\n", "14. 68\n", "15. 58\n", "16. 64\n", "17. 66\n", "18. 57\n", "19. 68\n", "20. 62\n", "21. 59\n", "22. 73\n", "23. 61\n", "24. 61\n", "25. 57\n", "26. 58\n", "27. 57\n", "28. 67\n", "29. 81\n", "30. 79\n", "31. 76\n", "32. 78\n", "33. 74\n", "34. 67\n", "35. 84\n", "36. 85\n", "37. 79\n", "38. 82\n", "39. 87\n", "40. 90\n", "41. 87\n", "42. 93\n", "43. 92\n", "44. 82\n", "45. 80\n", "46. 79\n", "47. 77\n", "48. 72\n", "49. 65\n", "50. 73\n", "51. 76\n", "52. 77\n", "53. 76\n", "54. 76\n", "55. 76\n", "56. 75\n", "57. 78\n", "58. 73\n", "59. 80\n", "60. 77\n", "61. 83\n", "62. 84\n", "63. 85\n", "64. 81\n", "65. 84\n", "66. 83\n", "67. 83\n", "68. 88\n", "69. 92\n", "70. 92\n", "71. 89\n", "72. 82\n", "73. 73\n", "74. 81\n", "75. 91\n", "76. 80\n", "77. 81\n", "78. 82\n", "79. 84\n", "80. 87\n", "81. 85\n", "82. 74\n", "83. 81\n", "84. 82\n", "85. 86\n", "86. 85\n", "87. 82\n", "88. 86\n", "89. 88\n", "90. 86\n", "91. 83\n", "92. 81\n", "93. 81\n", "94. 81\n", "95. 82\n", "96. 86\n", "97. 85\n", "98. 87\n", "99. 89\n", "100. 90\n", "101. 90\n", "102. 92\n", "103. 86\n", "104. 86\n", "105. 82\n", "106. 80\n", "107. 79\n", "108. 77\n", "109. 79\n", "110. 76\n", "111. 78\n", "112. 78\n", "113. 77\n", "114. 72\n", "115. 75\n", "116. 79\n", "117. 81\n", "118. 86\n", "119. 88\n", "120. 97\n", "121. 94\n", "122. 96\n", "123. 94\n", "124. 91\n", "125. 92\n", "126. 93\n", "127. 93\n", "128. 87\n", "129. 84\n", "130. 80\n", "131. 78\n", "132. 75\n", "133. 73\n", "134. 81\n", "135. 76\n", "136. 77\n", "137. 71\n", "138. 71\n", "139. 78\n", "140. 67\n", "141. 76\n", "142. 68\n", "143. 82\n", "144. 64\n", "145. 71\n", "146. 81\n", "147. 69\n", "148. 63\n", "149. 70\n", "150. 77\n", "151. 75\n", "152. 76\n", "153. 68\n", "\n", "\n" ], "text/plain": [ " [1] 67 72 74 62 56 66 65 59 61 69 74 69 66 68 58 64 66 57 68 62 59 73 61 61 57\n", " [26] 58 57 67 81 79 76 78 74 67 84 85 79 82 87 90 87 93 92 82 80 79 77 72 65 73\n", " [51] 76 77 76 76 76 75 78 73 80 77 83 84 85 81 84 83 83 88 92 92 89 82 73 81 91\n", " [76] 80 81 82 84 87 85 74 81 82 86 85 82 86 88 86 83 81 81 81 82 86 85 87 89 90\n", "[101] 90 92 86 86 82 80 79 77 79 76 78 78 77 72 75 79 81 86 88 97 94 96 94 91 92\n", "[126] 93 93 87 84 80 78 75 73 81 76 77 71 71 78 67 76 68 82 64 71 81 69 63 70 77\n", "[151] 75 76 68" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "airquality$Temp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```R\n", "for(i in 1:numdays1)\n", "ifelse(airquality$Ozone[i] <60 & airquality$Temp<80,goodair1[i] = 1,goodair1[i] = 0)\n", "#here it's not working because of \"=\" sign , let's change to different assignment operator <-\n", "```" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "for(i in 1:numdays)\n", " ifelse(airquality$Ozone[i] <60 && airquality$Temp<80,goodair[i] <- 1,goodair[i] <- 0)\n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19\n", "[20] 20 21 22 23 24 25 27 28 29 31 32 33 35 36 38 41 43 44 45\n", "[39] 46 47 48 49 50 51 54 55 57 59 60 61 63 64 67 72 73 74 75\n", "[58] 76 77 78 82 87 88 90 92 93 94 95 97 102 103 104 105 107 108 109\n", "[77] 110 111\n" ] } ], "source": [ "goodindices1 <- which(goodair == 1)\n", "#airquality[goodindices1,] \n", "print(goodindices1)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "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", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
OzoneSolar.RWindTempMonthDay
141 190 7.467 5 1
236 118 8.072 5 2
312 149 12.674 5 3
418 313 11.562 5 4
5NA NA 14.356 5 5
628 NA 14.966 5 6
723 299 8.665 5 7
819 99 13.859 5 8
9 8 19 20.161 5 9
10NA 194 8.669 5 10
11 7 NA 6.974 5 11
1216 256 9.769 5 12
1311 290 9.266 5 13
1414 274 10.968 5 14
1518 65 13.258 5 15
1614 334 11.564 5 16
1734 307 12.066 5 17
18 6 78 18.457 5 18
1930 322 11.568 5 19
2011 44 9.762 5 20
21 1 8 9.759 5 21
2211 320 16.673 5 22
23 4 25 9.761 5 23
2432 92 12.061 5 24
25NA 66 16.657 5 25
27NA NA 8.057 5 27
2823 13 12.067 5 28
2945 252 14.981 5 29
3137 279 7.476 5 31
32NA 286 8.678 6 1
.....................
60NA 31 14.977 6 29
61NA 138 8.083 6 30
6349 248 9.285 7 2
6432 236 9.281 7 3
6740 314 10.983 7 6
72NA 139 8.682 7 11
7310 264 14.373 7 12
7427 175 14.981 7 13
75NA 291 14.991 7 14
76 7 48 14.380 7 15
7748 260 6.981 7 16
7835 274 10.382 7 17
8216 7 6.974 7 21
8720 81 8.682 7 26
8852 82 12.086 7 27
9050 275 7.486 7 29
9259 254 9.281 7 31
9339 83 6.981 8 1
94 9 24 13.881 8 2
9516 77 7.482 8 3
9735 NA 7.485 8 5
102NA 222 8.692 8 10
103NA 137 11.586 8 11
10444 192 11.586 8 12
10528 273 11.582 8 13
107NA 64 11.579 8 15
10822 71 10.377 8 16
10959 51 6.379 8 17
11023 115 7.476 8 18
11131 244 10.978 8 19
\n" ], "text/latex": [ "\\begin{tabular}{r|llllll}\n", " & Ozone & Solar.R & Wind & Temp & Month & Day\\\\\n", "\\hline\n", "\t1 & 41 & 190 & 7.4 & 67 & 5 & 1 \\\\\n", "\t2 & 36 & 118 & 8.0 & 72 & 5 & 2 \\\\\n", "\t3 & 12 & 149 & 12.6 & 74 & 5 & 3 \\\\\n", "\t4 & 18 & 313 & 11.5 & 62 & 5 & 4 \\\\\n", "\t5 & NA & NA & 14.3 & 56 & 5 & 5 \\\\\n", "\t6 & 28 & NA & 14.9 & 66 & 5 & 6 \\\\\n", "\t7 & 23 & 299 & 8.6 & 65 & 5 & 7 \\\\\n", "\t8 & 19 & 99 & 13.8 & 59 & 5 & 8 \\\\\n", "\t9 & 8 & 19 & 20.1 & 61 & 5 & 9 \\\\\n", "\t10 & NA & 194 & 8.6 & 69 & 5 & 10 \\\\\n", "\t11 & 7 & NA & 6.9 & 74 & 5 & 11 \\\\\n", "\t12 & 16 & 256 & 9.7 & 69 & 5 & 12 \\\\\n", "\t13 & 11 & 290 & 9.2 & 66 & 5 & 13 \\\\\n", "\t14 & 14 & 274 & 10.9 & 68 & 5 & 14 \\\\\n", "\t15 & 18 & 65 & 13.2 & 58 & 5 & 15 \\\\\n", "\t16 & 14 & 334 & 11.5 & 64 & 5 & 16 \\\\\n", "\t17 & 34 & 307 & 12.0 & 66 & 5 & 17 \\\\\n", "\t18 & 6 & 78 & 18.4 & 57 & 5 & 18 \\\\\n", "\t19 & 30 & 322 & 11.5 & 68 & 5 & 19 \\\\\n", "\t20 & 11 & 44 & 9.7 & 62 & 5 & 20 \\\\\n", "\t21 & 1 & 8 & 9.7 & 59 & 5 & 21 \\\\\n", "\t22 & 11 & 320 & 16.6 & 73 & 5 & 22 \\\\\n", "\t23 & 4 & 25 & 9.7 & 61 & 5 & 23 \\\\\n", "\t24 & 32 & 92 & 12.0 & 61 & 5 & 24 \\\\\n", "\t25 & NA & 66 & 16.6 & 57 & 5 & 25 \\\\\n", "\t27 & NA & NA & 8.0 & 57 & 5 & 27 \\\\\n", "\t28 & 23 & 13 & 12.0 & 67 & 5 & 28 \\\\\n", "\t29 & 45 & 252 & 14.9 & 81 & 5 & 29 \\\\\n", "\t31 & 37 & 279 & 7.4 & 76 & 5 & 31 \\\\\n", "\t32 & NA & 286 & 8.6 & 78 & 6 & 1 \\\\\n", "\t... & ... & ... & ... & ... & ... & ...\\\\\n", "\t60 & NA & 31 & 14.9 & 77 & 6 & 29 \\\\\n", "\t61 & NA & 138 & 8.0 & 83 & 6 & 30 \\\\\n", "\t63 & 49 & 248 & 9.2 & 85 & 7 & 2 \\\\\n", "\t64 & 32 & 236 & 9.2 & 81 & 7 & 3 \\\\\n", "\t67 & 40 & 314 & 10.9 & 83 & 7 & 6 \\\\\n", "\t72 & NA & 139 & 8.6 & 82 & 7 & 11 \\\\\n", "\t73 & 10 & 264 & 14.3 & 73 & 7 & 12 \\\\\n", "\t74 & 27 & 175 & 14.9 & 81 & 7 & 13 \\\\\n", "\t75 & NA & 291 & 14.9 & 91 & 7 & 14 \\\\\n", "\t76 & 7 & 48 & 14.3 & 80 & 7 & 15 \\\\\n", "\t77 & 48 & 260 & 6.9 & 81 & 7 & 16 \\\\\n", "\t78 & 35 & 274 & 10.3 & 82 & 7 & 17 \\\\\n", "\t82 & 16 & 7 & 6.9 & 74 & 7 & 21 \\\\\n", "\t87 & 20 & 81 & 8.6 & 82 & 7 & 26 \\\\\n", "\t88 & 52 & 82 & 12.0 & 86 & 7 & 27 \\\\\n", "\t90 & 50 & 275 & 7.4 & 86 & 7 & 29 \\\\\n", "\t92 & 59 & 254 & 9.2 & 81 & 7 & 31 \\\\\n", "\t93 & 39 & 83 & 6.9 & 81 & 8 & 1 \\\\\n", "\t94 & 9 & 24 & 13.8 & 81 & 8 & 2 \\\\\n", "\t95 & 16 & 77 & 7.4 & 82 & 8 & 3 \\\\\n", "\t97 & 35 & NA & 7.4 & 85 & 8 & 5 \\\\\n", "\t102 & NA & 222 & 8.6 & 92 & 8 & 10 \\\\\n", "\t103 & NA & 137 & 11.5 & 86 & 8 & 11 \\\\\n", "\t104 & 44 & 192 & 11.5 & 86 & 8 & 12 \\\\\n", "\t105 & 28 & 273 & 11.5 & 82 & 8 & 13 \\\\\n", "\t107 & NA & 64 & 11.5 & 79 & 8 & 15 \\\\\n", "\t108 & 22 & 71 & 10.3 & 77 & 8 & 16 \\\\\n", "\t109 & 59 & 51 & 6.3 & 79 & 8 & 17 \\\\\n", "\t110 & 23 & 115 & 7.4 & 76 & 8 & 18 \\\\\n", "\t111 & 31 & 244 & 10.9 & 78 & 8 & 19 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | Ozone | Solar.R | Wind | Temp | Month | Day | \n", "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n", "| 1 | 41 | 190 | 7.4 | 67 | 5 | 1 | \n", "| 2 | 36 | 118 | 8.0 | 72 | 5 | 2 | \n", "| 3 | 12 | 149 | 12.6 | 74 | 5 | 3 | \n", "| 4 | 18 | 313 | 11.5 | 62 | 5 | 4 | \n", "| 5 | NA | NA | 14.3 | 56 | 5 | 5 | \n", "| 6 | 28 | NA | 14.9 | 66 | 5 | 6 | \n", "| 7 | 23 | 299 | 8.6 | 65 | 5 | 7 | \n", "| 8 | 19 | 99 | 13.8 | 59 | 5 | 8 | \n", "| 9 | 8 | 19 | 20.1 | 61 | 5 | 9 | \n", "| 10 | NA | 194 | 8.6 | 69 | 5 | 10 | \n", "| 11 | 7 | NA | 6.9 | 74 | 5 | 11 | \n", "| 12 | 16 | 256 | 9.7 | 69 | 5 | 12 | \n", "| 13 | 11 | 290 | 9.2 | 66 | 5 | 13 | \n", "| 14 | 14 | 274 | 10.9 | 68 | 5 | 14 | \n", "| 15 | 18 | 65 | 13.2 | 58 | 5 | 15 | \n", "| 16 | 14 | 334 | 11.5 | 64 | 5 | 16 | \n", "| 17 | 34 | 307 | 12.0 | 66 | 5 | 17 | \n", "| 18 | 6 | 78 | 18.4 | 57 | 5 | 18 | \n", "| 19 | 30 | 322 | 11.5 | 68 | 5 | 19 | \n", "| 20 | 11 | 44 | 9.7 | 62 | 5 | 20 | \n", "| 21 | 1 | 8 | 9.7 | 59 | 5 | 21 | \n", "| 22 | 11 | 320 | 16.6 | 73 | 5 | 22 | \n", "| 23 | 4 | 25 | 9.7 | 61 | 5 | 23 | \n", "| 24 | 32 | 92 | 12.0 | 61 | 5 | 24 | \n", "| 25 | NA | 66 | 16.6 | 57 | 5 | 25 | \n", "| 27 | NA | NA | 8.0 | 57 | 5 | 27 | \n", "| 28 | 23 | 13 | 12.0 | 67 | 5 | 28 | \n", "| 29 | 45 | 252 | 14.9 | 81 | 5 | 29 | \n", "| 31 | 37 | 279 | 7.4 | 76 | 5 | 31 | \n", "| 32 | NA | 286 | 8.6 | 78 | 6 | 1 | \n", "| ... | ... | ... | ... | ... | ... | ... | \n", "| 60 | NA | 31 | 14.9 | 77 | 6 | 29 | \n", "| 61 | NA | 138 | 8.0 | 83 | 6 | 30 | \n", "| 63 | 49 | 248 | 9.2 | 85 | 7 | 2 | \n", "| 64 | 32 | 236 | 9.2 | 81 | 7 | 3 | \n", "| 67 | 40 | 314 | 10.9 | 83 | 7 | 6 | \n", "| 72 | NA | 139 | 8.6 | 82 | 7 | 11 | \n", "| 73 | 10 | 264 | 14.3 | 73 | 7 | 12 | \n", "| 74 | 27 | 175 | 14.9 | 81 | 7 | 13 | \n", "| 75 | NA | 291 | 14.9 | 91 | 7 | 14 | \n", "| 76 | 7 | 48 | 14.3 | 80 | 7 | 15 | \n", "| 77 | 48 | 260 | 6.9 | 81 | 7 | 16 | \n", "| 78 | 35 | 274 | 10.3 | 82 | 7 | 17 | \n", "| 82 | 16 | 7 | 6.9 | 74 | 7 | 21 | \n", "| 87 | 20 | 81 | 8.6 | 82 | 7 | 26 | \n", "| 88 | 52 | 82 | 12.0 | 86 | 7 | 27 | \n", "| 90 | 50 | 275 | 7.4 | 86 | 7 | 29 | \n", "| 92 | 59 | 254 | 9.2 | 81 | 7 | 31 | \n", "| 93 | 39 | 83 | 6.9 | 81 | 8 | 1 | \n", "| 94 | 9 | 24 | 13.8 | 81 | 8 | 2 | \n", "| 95 | 16 | 77 | 7.4 | 82 | 8 | 3 | \n", "| 97 | 35 | NA | 7.4 | 85 | 8 | 5 | \n", "| 102 | NA | 222 | 8.6 | 92 | 8 | 10 | \n", "| 103 | NA | 137 | 11.5 | 86 | 8 | 11 | \n", "| 104 | 44 | 192 | 11.5 | 86 | 8 | 12 | \n", "| 105 | 28 | 273 | 11.5 | 82 | 8 | 13 | \n", "| 107 | NA | 64 | 11.5 | 79 | 8 | 15 | \n", "| 108 | 22 | 71 | 10.3 | 77 | 8 | 16 | \n", "| 109 | 59 | 51 | 6.3 | 79 | 8 | 17 | \n", "| 110 | 23 | 115 | 7.4 | 76 | 8 | 18 | \n", "| 111 | 31 | 244 | 10.9 | 78 | 8 | 19 | \n", "\n", "\n" ], "text/plain": [ " Ozone Solar.R Wind Temp Month Day\n", "1 41 190 7.4 67 5 1 \n", "2 36 118 8.0 72 5 2 \n", "3 12 149 12.6 74 5 3 \n", "4 18 313 11.5 62 5 4 \n", "5 NA NA 14.3 56 5 5 \n", "6 28 NA 14.9 66 5 6 \n", "7 23 299 8.6 65 5 7 \n", "8 19 99 13.8 59 5 8 \n", "9 8 19 20.1 61 5 9 \n", "10 NA 194 8.6 69 5 10 \n", "11 7 NA 6.9 74 5 11 \n", "12 16 256 9.7 69 5 12 \n", "13 11 290 9.2 66 5 13 \n", "14 14 274 10.9 68 5 14 \n", "15 18 65 13.2 58 5 15 \n", "16 14 334 11.5 64 5 16 \n", "17 34 307 12.0 66 5 17 \n", "18 6 78 18.4 57 5 18 \n", "19 30 322 11.5 68 5 19 \n", "20 11 44 9.7 62 5 20 \n", "21 1 8 9.7 59 5 21 \n", "22 11 320 16.6 73 5 22 \n", "23 4 25 9.7 61 5 23 \n", "24 32 92 12.0 61 5 24 \n", "25 NA 66 16.6 57 5 25 \n", "27 NA NA 8.0 57 5 27 \n", "28 23 13 12.0 67 5 28 \n", "29 45 252 14.9 81 5 29 \n", "31 37 279 7.4 76 5 31 \n", "32 NA 286 8.6 78 6 1 \n", "... ... ... ... ... ... ...\n", "60 NA 31 14.9 77 6 29 \n", "61 NA 138 8.0 83 6 30 \n", "63 49 248 9.2 85 7 2 \n", "64 32 236 9.2 81 7 3 \n", "67 40 314 10.9 83 7 6 \n", "72 NA 139 8.6 82 7 11 \n", "73 10 264 14.3 73 7 12 \n", "74 27 175 14.9 81 7 13 \n", "75 NA 291 14.9 91 7 14 \n", "76 7 48 14.3 80 7 15 \n", "77 48 260 6.9 81 7 16 \n", "78 35 274 10.3 82 7 17 \n", "82 16 7 6.9 74 7 21 \n", "87 20 81 8.6 82 7 26 \n", "88 52 82 12.0 86 7 27 \n", "90 50 275 7.4 86 7 29 \n", "92 59 254 9.2 81 7 31 \n", "93 39 83 6.9 81 8 1 \n", "94 9 24 13.8 81 8 2 \n", "95 16 77 7.4 82 8 3 \n", "97 35 NA 7.4 85 8 5 \n", "102 NA 222 8.6 92 8 10 \n", "103 NA 137 11.5 86 8 11 \n", "104 44 192 11.5 86 8 12 \n", "105 28 273 11.5 82 8 13 \n", "107 NA 64 11.5 79 8 15 \n", "108 22 71 10.3 77 8 16 \n", "109 59 51 6.3 79 8 17 \n", "110 23 115 7.4 76 8 18 \n", "111 31 244 10.9 78 8 19 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "airquality[goodindices1,]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "#export and save the result into working directory with file name as goodquality.txt\n", "write.table(airquality[goodindices1,], \"goodquality.txt\", sep=\",\")" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "data": { "text/html": [ "
    \n", "\t
  1. 'Data Science Process.pdf'
  2. \n", "\t
  3. 'goodquality.txt'
  4. \n", "\t
  5. 'README.md'
  6. \n", "\t
  7. 'sphweb_Stats.ipynb'
  8. \n", "
\n" ], "text/latex": [ "\\begin{enumerate*}\n", "\\item 'Data Science Process.pdf'\n", "\\item 'goodquality.txt'\n", "\\item 'README.md'\n", "\\item 'sphweb\\_Stats.ipynb'\n", "\\end{enumerate*}\n" ], "text/markdown": [ "1. 'Data Science Process.pdf'\n", "2. 'goodquality.txt'\n", "3. 'README.md'\n", "4. 'sphweb_Stats.ipynb'\n", "\n", "\n" ], "text/plain": [ "[1] \"Data Science Process.pdf\" \"goodquality.txt\" \n", "[3] \"README.md\" \"sphweb_Stats.ipynb\" " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# check whether the file exported into working directory\n", "list.files() " ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [], "source": [ "#open the file and check the data\n", "file.edit('goodquality.txt')" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "run_control": { "frozen": false, "read_only": false } }, "source": [ "### To open a pdf file from working directory using package from bioconductor ( general topic) \n", "Use the biocLite.R script to install Bioconductor packages. \n", "To install core packages, type the following in an R command window:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "```R\n", "source(\"https://bioconductor.org/biocLite.R\")\n", "biocLite(\"Biobase\")\n", "#to install packages from bioconductor\n", "#try http:// if https:// URLs are not supported\n", "```" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false }, "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading required package: BiocGenerics\n", "Loading required package: parallel\n", "\n", "Attaching package: 'BiocGenerics'\n", "\n", "The following objects are masked from 'package:parallel':\n", "\n", " clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,\n", " clusterExport, clusterMap, parApply, parCapply, parLapply,\n", " parLapplyLB, parRapply, parSapply, parSapplyLB\n", "\n", "The following objects are masked from 'package:stats':\n", "\n", " IQR, mad, xtabs\n", "\n", "The following objects are masked from 'package:base':\n", "\n", " anyDuplicated, append, as.data.frame, cbind, colnames, do.call,\n", " duplicated, eval, evalq, Filter, Find, get, grep, grepl, intersect,\n", " is.unsorted, lapply, lengths, Map, mapply, match, mget, order,\n", " paste, pmax, pmax.int, pmin, pmin.int, Position, rank, rbind,\n", " Reduce, rownames, sapply, setdiff, sort, table, tapply, union,\n", " unique, unsplit\n", "\n", "Welcome to Bioconductor\n", "\n", " Vignettes contain introductory material; view with\n", " 'browseVignettes()'. To cite Bioconductor, see\n", " 'citation(\"Biobase\")', and for packages 'citation(\"pkgname\")'.\n", "\n" ] }, { "data": { "text/html": [ "TRUE" ], "text/latex": [ "TRUE" ], "text/markdown": [ "TRUE" ], "text/plain": [ "[1] TRUE" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "library(Biobase)\n", "openPDF(\"Data Science Process.pdf\") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### While Statements & REPEAT\n", "Similar to a loop function, the while statement can be used to perform an operation while a given condition is true. For example:\n" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 2\n", "[1] 4\n", "[1] 6\n" ] } ], "source": [ "z <- 0\n", "\n", "while (z < 5) {\n", " \n", " z <- z + 2\n", " \n", " print(z)\n", " \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above while statement we initiate z to have a value of 0. We then state\n", "that as long as z is less than 5 we will continue to perform the following loop\n", "operation z<-z+2.\n", "Thus we have\n", "```R\n", "z <- 0+2 ##Initially z is 0, but after the first iteration of the loop the value of z is 2\n", "\n", "z <- 2+2 ## After the second iteration the value of z is 4\n", "\n", "z <- 4+2 ## After the third iteration the value of z is 6\n", "```\n", "The while statement stops here because now z is now bigger than 5." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### repeat exercies" ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 2\n", "[1] 3\n", "[1] 4\n", "[1] 5\n", "[1] 6\n", "[1] 7\n", "[1] 8\n", "[1] 9\n", "[1] 10\n", "[1] 11\n", "[1] 12\n", "[1] 13\n", "[1] 14\n", "[1] 15\n" ] } ], "source": [ "#Another option for looping is the repeat function. An example follows:\n", "i<-1\n", "\n", "repeat{\n", "\n", " print(i)\n", "\n", " if( i == 15) break\n", "\n", " i<-i+1\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false } }, "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", "repeat{\n", " print(x)\n", " x <- x+1\n", " if (x == 6){\n", " break\n", " }\n", " \n", "}" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false, "run_control": { "frozen": false, "read_only": false }, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 2\n", "[1] 4\n", "[1] 6\n", "[1] 8\n", "[1] 10\n" ] } ], "source": [ "# For the first exercise, write a repeat{} loop that prints all the even numbers\n", "# from 2 10, via incrementing the variable, i <- 0.\n", "i <- 0\n", "repeat {\n", " i <- i + 2\n", " print(i)\n", " if (i == 10) \n", " break\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Using the following variables: \n", "```R \n", "msg <- c('Hello') \n", "i <- 1 \n", "``` \n", "Write a repeat{} loop that breaks off the incrementation of, “i“, after 5 loops, andz prints “msg” at every increment.\n" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n" ] } ], "source": [ "msg <- c(\"Hello\")\n", "i <- 1\n", "\n", "repeat {\n", " i <- i + 1\n", " print(msg)\n", " if (i == 5) {break} \n", "}" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n" ] } ], "source": [ "msg <- c('Hello')\n", "i <- 1 \n", "repeat{\n", "print(msg)\n", "i <- i +1\n", "if (i == 5)break\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### while exercises" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 3\n", "[1] 5\n", "[1] 7\n" ] } ], "source": [ "#With, i <- 1, write a while() loop that prints the odd numbers from 1 through 7.\n", "i <- 1\n", "while(i<=7){\n", " print(i)\n", " i <- i +2 \n", "}" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n", "[1] \"Hello\"\n" ] } ], "source": [ "#Write a while() loop that increments the variable, “i“, 6 times, and prints “msg” at every iteration.\n", "i <- 1\n", "while(i<=6){\n", " print(msg)\n", " i <- i +1\n", "}\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### for loop exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### examples:\n", "```R\n", "for(i in 1:4) {\n", "print(\"variable\"[i])\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 15\n", "[1] 23\n", "[1] 78\n", "[1] 45\n" ] } ], "source": [ "a <- c(15,23,78,45,124,82,75)\n", "for (i in 1:4){\n", " print(a[i])\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```R\n", "for(i in seq(\"variable\")) {\n", "print(i)\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 1\n", "[1] 2\n", "[1] 3\n", "[1] 4\n", "[1] 5\n", "[1] 6\n", "[1] 7\n" ] } ], "source": [ "for (i in seq(a)){\n", " print(i)\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```R\n", "for(i in seq_along(\"variable\")) {\n", "print(\"variable\"[i])\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 15\n", "[1] 23\n", "[1] 78\n", "[1] 45\n", "[1] 124\n", "[1] 82\n", "[1] 75\n" ] } ], "source": [ "for (i in seq_along(a)){\n", " print(a[i])\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```R\n", "for(letter in \"variable\") {\n", "print(letter)\n", "```" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1] 2\n" ] } ], "source": [ "a <- \"Last Checkpoint: 16 hours ago (unsaved changes)\"\n", "for (letter in 2){\n", " print(letter)\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ " # Create a vector filled with random normal values\n", "u1 <- rnorm(30)\n", "print(\"This loop calculates the square of the first 10 elements of vector u1\")\n", "\n", "# Initialize `usq`\n", "usq <- 0\n", "\n", "for(i in 1:10) {\n", " # i-th element of `u1` squared into `i`-th position of `usq`\n", " usq[i] <- u1[i]*u1[i]\n", " print(usq[i])\n", "}\n", "\n", "print(i)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "R [R-Env]", "language": "R", "name": "R [R-Env]" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.3.2" } }, "nbformat": 4, "nbformat_minor": 0 }