About the Data

Exercise 1: Loops

a 1.1 Create a loop that iterates over the number 1 to 12 and print the square of each value

[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
[1] 49
[1] 64
[1] 81
[1] 100
[1] 121
[1] 144
  1. 1.2 Create a loop that iterates over the numbers 1 to 100 and only prints even numbers
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 16
[1] 18
[1] 20
[1] 22
[1] 24
[1] 26
[1] 28
[1] 30
[1] 32
[1] 34
[1] 36
[1] 38
[1] 40
[1] 42
[1] 44
[1] 46
[1] 48
[1] 50
[1] 52
[1] 54
[1] 56
[1] 58
[1] 60
[1] 62
[1] 64
[1] 66
[1] 68
[1] 70
[1] 72
[1] 74
[1] 76
[1] 78
[1] 80
[1] 82
[1] 84
[1] 86
[1] 88
[1] 90
[1] 92
[1] 94
[1] 96
[1] 98
[1] 100
  1. 1.3 Create a loop that adds 1/2 to each element of a vector where the first element is 2. Stop when the loop reaches 20
 [1]  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0  7.5  8.0  8.5  9.0
[16]  9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5
[31] 17.0 17.5 18.0 18.5 19.0 19.5 20.0
  1. Create a while loop that starts with the value of 0.5 and add one fourth of the last value on each iteration Repeat this loop while the value is less than 40.
[1] 0.5
[1] 0.625
[1] 0.78125
[1] 0.9765625
[1] 1.220703
[1] 1.525879
[1] 1.907349
[1] 2.384186
[1] 2.980232
[1] 3.72529
[1] 4.656613
[1] 5.820766
[1] 7.275958
[1] 9.094947
[1] 11.36868
[1] 14.21085
[1] 17.76357
[1] 22.20446
[1] 27.75558
[1] 34.69447

Exercise 2: Applying Loops to a Dataset

  • In this exercise, we will use the College dataset
  • All questions need to be answered using loops
  1. 2.1 Use a Loop to compute the variance of every column in College.csv, print the results in a vector. If the variable is not numeric (or integer) print the string “no variance”.
 [1] "no variance"      "no variance"      "14978459.5301251" "6007959.69879526"
 [5] "863368.392309836" "311.182455651528" "392.229215592618" "23526579.3264538"
 [9] "2317798.85145418" "16184661.6314367" "1202743.02797569" "27259.779945999" 
[13] "458425.753267258" "266.608635513275" "216.747840624129" "15.6685278761825"
[17] "153.556744152105" "27266865.6394771" "295.073717310831" "no variance"     
[21] "no variance"     
  1. 2.2 Compute the number of unique values in each column of College.csv
 [1] 777   2 711 693 581  82  89 714 566 640 553 122 294  78  65 173  61 744  81
[20]   2   2
  1. 2.3 Generate (using a loop) 20 random normally distributed vectors (of length 100 each) with mean 100 and sd 10

Save the mean minus the median of each randomly generated vector inside a new vector (Use set.seed(28))

 [1]  1.75261330  1.80825286 -1.29377663  1.22513780 -1.04835404  0.07878913
 [7] -1.82175663 -0.60195083 -1.19990087 -1.19068782  2.04934384 -1.34000940
[13] -1.65250370  0.55954496 -1.16188787 -0.84151065  1.07363336 -2.32332294
[19]  1.11157950  0.04375424
  1. 2.4 Using a for loop, create an algorithm that returns the lowest outlier for each numeric (or integer) variable. If the variable does not have an outlier, it should return the string “No outliers”. If the variable is not numeric (or integer), print the string not numeric/integer
 [1] "not numeric/integer" "not numeric/integer" "8000"               
 [4] "5200"                "1902"                "66"                 
 [7] "No outliers"         "8528"                "2281"               
[10] "21700"               "7262"                "96"                 
[13] "3000"                "8"                   "24"                 
[16] "2.5"                 "60"                  "17007"              
[19] "10"                  "not numeric/integer" "not numeric/integer"

Exercise 3. Generate the Following Series (using loops), print 100 iterations of each:

  1. 3.1 Harmonic Series: \(\frac{1}{1} + \frac{1}{2} + \frac{1}{3} + ... \rightarrow \infty\)
  [1] 0.000000 1.000000 1.500000 1.833333 2.083333 2.283333 2.450000 2.592857
  [9] 2.717857 2.828968 2.928968 3.019877 3.103211 3.180134 3.251562 3.318229
 [17] 3.380729 3.439553 3.495108 3.547740 3.597740 3.645359 3.690813 3.734292
 [25] 3.775958 3.815958 3.854420 3.891457 3.927171 3.961654 3.994987 4.027245
 [33] 4.058495 4.088798 4.118210 4.146781 4.174559 4.201586 4.227902 4.253543
 [41] 4.278543 4.302933 4.326743 4.349999 4.372726 4.394948 4.416687 4.437964
 [49] 4.458797 4.479205 4.499205 4.518813 4.538044 4.556912 4.575430 4.593612
 [57] 4.611469 4.629013 4.646255 4.663204 4.679870 4.696264 4.712393 4.728266
 [65] 4.743891 4.759276 4.774427 4.789352 4.804058 4.818551 4.832837 4.846921
 [73] 4.860810 4.874509 4.888022 4.901356 4.914514 4.927501 4.940321 4.952979
 [81] 4.965479 4.977825 4.990020 5.002068 5.013973 5.025738 5.037366 5.048860
 [89] 5.060224 5.071459 5.082571 5.093560 5.104429 5.115182 5.125820 5.136346
 [97] 5.146763 5.157072 5.167277 5.177378 5.187378
  1. 3.2 Sum of Reciprocals of Square Numbers: \(\frac{1}{1} + \frac{1}{4} + \frac{1}{9} + ... = \frac{\pi}{6}\)
  [1] 0.000000 1.000000 1.250000 1.361111 1.423611 1.463611 1.491389 1.511797
  [9] 1.527422 1.539768 1.549768 1.558032 1.564977 1.570894 1.575996 1.580440
 [17] 1.584347 1.587807 1.590893 1.593663 1.596163 1.598431 1.600497 1.602387
 [25] 1.604123 1.605723 1.607203 1.608574 1.609850 1.611039 1.612150 1.613191
 [33] 1.614167 1.615086 1.615951 1.616767 1.617539 1.618269 1.618962 1.619619
 [41] 1.620244 1.620839 1.621406 1.621947 1.622463 1.622957 1.623430 1.623882
 [49] 1.624316 1.624733 1.625133 1.625517 1.625887 1.626243 1.626586 1.626917
 [57] 1.627235 1.627543 1.627840 1.628128 1.628406 1.628674 1.628934 1.629186
 [65] 1.629431 1.629667 1.629897 1.630120 1.630336 1.630546 1.630750 1.630948
 [73] 1.631141 1.631329 1.631511 1.631689 1.631862 1.632031 1.632195 1.632356
 [81] 1.632512 1.632664 1.632813 1.632958 1.633100 1.633238 1.633374 1.633506
 [89] 1.633635 1.633761 1.633884 1.634005 1.634123 1.634239 1.634352 1.634463
 [97] 1.634571 1.634678 1.634782 1.634884 1.634984
  1. 3.3 Sum of Reciprocals of the power of any \(n > 1\): \(\frac{1}{1} + \frac{1}{n} + \frac{1}{n^2} + \frac{1}{n^3} + ... = \frac{n}{n-1}\)

Do it once for n = 2 and another for n = 10

  [1] 1.000000 1.500000 1.750000 1.875000 1.937500 1.968750 1.984375 1.992188
  [9] 1.996094 1.998047 1.999023 1.999512 1.999756 1.999878 1.999939 1.999969
 [17] 1.999985 1.999992 1.999996 1.999998 1.999999 2.000000 2.000000 2.000000
 [25] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [33] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [41] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [49] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [57] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [65] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [73] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [81] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [89] 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000 2.000000
 [97] 2.000000 2.000000 2.000000 2.000000 2.000000

  [1] 1.000000 1.100000 1.110000 1.111000 1.111100 1.111110 1.111111 1.111111
  [9] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [17] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [25] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [33] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [41] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [49] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [57] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [65] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [73] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [81] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [89] 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111 1.111111
 [97] 1.111111 1.111111 1.111111 1.111111 1.111111
  1. 3.4 Grandi’s Series: \(1 - 1 + 1 - 1 + 1 - ... = \sum_{n=0}^{\infty}(-1)^n\)
  [1] 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
 [38] 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
 [75] 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

Exercise 4. Theorethical Questions

  1. In which recursion problems is best to use a while rather than a for loop.

  2. Define what is an algorithm? and what makes it different from a mathematical operation?

  3. In which cases is worth to invest time in building your own algorithms.